Code-Tuning Strategies

Code Complete
Author : Steven C. McConnell.

Prof. Asha N

1
Code-Tuning Strategies

1.
2.

3.
4.
5.

Performance Overview
Introduction to Code Tuning
Kinds of Fat and Molasses
Measurement
Iteration

Prof. Asha N

2
1. Performance Overview


Code tuning is one way of improving a program’s performance
 Quality Characteristics and Performance






“Performance is only loosely related to code speed”. To the extent
that you work on your code’s speed, you’re not working on other
quality characteristics.
work on speed may hurt performance rather than help it

Performance and Code Tuning


When efficiency is chosen as a priority - think about efficiency from
each of these view points








Program Requirements
Program Design
Class and Routine Design
Operating-System Interactions
Code Compilation
Hardware
Code Tuning

Prof. Asha N

3
2. Introduction to Code Tuning


Code Tuning - mastering the art of writing
efficient code, One attraction is that a routine
that executes in 20 microseconds, tweak a
few lines, and reduce the execution speed to
2 microseconds

Prof. Asha N

4
Contd…


The Pareto Principle




The Pareto Principle also known as the 80/20 rule, states
that you can get 80 percent of the result with 20 percent of
the effort. The principle applies to a lot of areas other than
programming, but it definitely applies to program
optimization.

Old Wives’ Tales


some common misapprehensions about code tuning


Reducing the lines of code in a high-level language
improves the speed or size of the resulting machine
code—false!

Prof. Asha N

5
Contd….
Consider the following code that initializes a 10-element
array:
(a) for i = 1 to 10
a[ i ] = I
end for
(b) a[ 1 ] = 1
a[ 2 ] = 2
a[ 3 ] = 3
a[ 4 ] = 4
a[ 5 ] = 5
a[ 6 ] = 6
a[ 7 ] = 7
a[ 8 ] = 8
a[ 9 ] = 9
a[ 10 ] = 10

Prof. Asha N

6
Contd….


Certain operations are probably faster or smaller than
others—false!




What was true on one machine with one set of tools can easily be
false on another machine with a different set of tools.

You should optimize as you go—false!


Focusing on optimization during initial development detracts from
achieving other program objectives.



A fast program is just as important as a correct one—false!



When to Tune





It’s hardly ever true that programs need to be fast or small before
they need to be correct

Use a high-quality design. Make the program right. Make it
modular and easily modifiable so that it’s easy to work on later.
When it’s complete and correct, check the performance. If the
program lumbers, make it fast and small. Don’t optimize until you
know you need to.

Prof. Asha N

7
Contd….


Compiler Optimizations

Prof. Asha N

8
3. Kinds of Fat and Molasses




In code tuning you have to profile the
program to know with any confidence which
parts are slow and fat
Common Sources of Inefficiency







Input/output operations
Paging
System calls
Interpreted languages
Errors
Prof. Asha N

9
4. Measurement




small parts of a program usually consume a
disproportionate share of the run time, So
measure your code to find the hot spots.
Once you’ve found the hot spots and
optimized them, measure the code again to
assess how much you’ve improved it

Prof. Asha N

10
Contd…


C++ Example of Straightforward Code to
Sum the Elements in a Matrix
sum = 0;
for ( row = 0; row < rowCount; row++ ) {
for ( column = 0; column < columnCount; column++ ) {
sum = sum + matrix[ row ][ column ];
}
}

Prof. Asha N

11
Contd…


C++ Example of an Attempt to Tune Code
to sum the Elements in a Matrix
sum = 0;
elementPointer = matrix;
lastElementPointer = matrix[ rowCount - 1 ][ columnCount - 1 ] + 1;
while ( elementPointer < lastElementPointer ) {
sum = sum + *elementPointer++;
}

Prof. Asha N

12
5. Iteration


keep trying until u improve to an extent.

Prof. Asha N

13
Contd….

Prof. Asha N

14

Code tuning strategies

  • 1.
    Code-Tuning Strategies Code Complete Author: Steven C. McConnell. Prof. Asha N 1
  • 2.
    Code-Tuning Strategies 1. 2. 3. 4. 5. Performance Overview Introductionto Code Tuning Kinds of Fat and Molasses Measurement Iteration Prof. Asha N 2
  • 3.
    1. Performance Overview  Codetuning is one way of improving a program’s performance  Quality Characteristics and Performance    “Performance is only loosely related to code speed”. To the extent that you work on your code’s speed, you’re not working on other quality characteristics. work on speed may hurt performance rather than help it Performance and Code Tuning  When efficiency is chosen as a priority - think about efficiency from each of these view points        Program Requirements Program Design Class and Routine Design Operating-System Interactions Code Compilation Hardware Code Tuning Prof. Asha N 3
  • 4.
    2. Introduction toCode Tuning  Code Tuning - mastering the art of writing efficient code, One attraction is that a routine that executes in 20 microseconds, tweak a few lines, and reduce the execution speed to 2 microseconds Prof. Asha N 4
  • 5.
    Contd…  The Pareto Principle   ThePareto Principle also known as the 80/20 rule, states that you can get 80 percent of the result with 20 percent of the effort. The principle applies to a lot of areas other than programming, but it definitely applies to program optimization. Old Wives’ Tales  some common misapprehensions about code tuning  Reducing the lines of code in a high-level language improves the speed or size of the resulting machine code—false! Prof. Asha N 5
  • 6.
    Contd…. Consider the followingcode that initializes a 10-element array: (a) for i = 1 to 10 a[ i ] = I end for (b) a[ 1 ] = 1 a[ 2 ] = 2 a[ 3 ] = 3 a[ 4 ] = 4 a[ 5 ] = 5 a[ 6 ] = 6 a[ 7 ] = 7 a[ 8 ] = 8 a[ 9 ] = 9 a[ 10 ] = 10 Prof. Asha N 6
  • 7.
    Contd….  Certain operations areprobably faster or smaller than others—false!   What was true on one machine with one set of tools can easily be false on another machine with a different set of tools. You should optimize as you go—false!  Focusing on optimization during initial development detracts from achieving other program objectives.  A fast program is just as important as a correct one—false!  When to Tune   It’s hardly ever true that programs need to be fast or small before they need to be correct Use a high-quality design. Make the program right. Make it modular and easily modifiable so that it’s easy to work on later. When it’s complete and correct, check the performance. If the program lumbers, make it fast and small. Don’t optimize until you know you need to. Prof. Asha N 7
  • 8.
  • 9.
    3. Kinds ofFat and Molasses   In code tuning you have to profile the program to know with any confidence which parts are slow and fat Common Sources of Inefficiency      Input/output operations Paging System calls Interpreted languages Errors Prof. Asha N 9
  • 10.
    4. Measurement   small partsof a program usually consume a disproportionate share of the run time, So measure your code to find the hot spots. Once you’ve found the hot spots and optimized them, measure the code again to assess how much you’ve improved it Prof. Asha N 10
  • 11.
    Contd…  C++ Example ofStraightforward Code to Sum the Elements in a Matrix sum = 0; for ( row = 0; row < rowCount; row++ ) { for ( column = 0; column < columnCount; column++ ) { sum = sum + matrix[ row ][ column ]; } } Prof. Asha N 11
  • 12.
    Contd…  C++ Example ofan Attempt to Tune Code to sum the Elements in a Matrix sum = 0; elementPointer = matrix; lastElementPointer = matrix[ rowCount - 1 ][ columnCount - 1 ] + 1; while ( elementPointer < lastElementPointer ) { sum = sum + *elementPointer++; } Prof. Asha N 12
  • 13.
    5. Iteration  keep tryinguntil u improve to an extent. Prof. Asha N 13
  • 14.