14

Click here to load reader

Code tuning strategies

Embed Size (px)

DESCRIPTION

Code tuning strategies, Code Complete Author : Steven C. McConnell.

Citation preview

Page 1: Code tuning strategies

Prof. Asha N 1

Code-Tuning Strategies

Code Complete

Author : Steven C. McConnell.

Page 2: Code tuning strategies

Prof. Asha N 2

Code-Tuning Strategies

1. Performance Overview

2. Introduction to Code Tuning

3. Kinds of Fat and Molasses

4. Measurement

5. Iteration

Page 3: Code tuning strategies

Prof. Asha N 3

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

Page 4: Code tuning strategies

Prof. Asha N 4

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

Page 5: Code tuning strategies

Prof. Asha N 5

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!

Page 6: Code tuning strategies

Prof. Asha N 6

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

Page 7: Code tuning strategies

Prof. Asha N 7

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! It’s hardly ever true that programs need to be fast or small before

they need to be correct

When to Tune 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.

Page 8: Code tuning strategies

Prof. Asha N 8

Contd….

Compiler Optimizations

Page 9: Code tuning strategies

Prof. Asha N 9

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

Page 10: Code tuning strategies

Prof. Asha N 10

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

Page 11: Code tuning strategies

Prof. Asha N 11

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 ];

}

}

Page 12: Code tuning strategies

Prof. Asha N 12

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++;

}

Page 13: Code tuning strategies

Prof. Asha N 13

5. Iteration

keep trying until u improve to an extent.

Page 14: Code tuning strategies

Prof. Asha N 14

Contd….