Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tori Bowman CSSE 375, Rose-Hulman October 22, 2007 1 Code Tuning (Chapters 25-26 of Code Complete)

Similar presentations


Presentation on theme: "Tori Bowman CSSE 375, Rose-Hulman October 22, 2007 1 Code Tuning (Chapters 25-26 of Code Complete)"— Presentation transcript:

1 Tori Bowman CSSE 375, Rose-Hulman October 22, 2007 1 Code Tuning (Chapters 25-26 of Code Complete)

2 Outline 2 State of 375 This Code Tuning Strategies Code Tuning Techniques

3 State of 375 3 Tonight HW 5 due 11:55 pm Tomorrow: part lecture/part work time Thursday: Week one progress report Friday: In class work time

4 4 Code Tuning Strategies

5 Overview 5 Code-tuning is one method of improving a programs performance through various techniques Performance is loosely related to speed When you work on speed, youre not working on other quality attributes

6 Historical context 6 1960s: Resources severely limited Nothing more important than efficiency 1970s: Computing improves Realizing how much it hurt readability and maintainability 1980s: Microcomputers brought efficiency issues back Waned in throughout the 1990s 2000s: Memory in embedded issues becomes the efficiency concern

7 Efficiency viewpoints 7 Program requirements Program design Class and routine design Operating-system interactions Code compilation Hardware Code tuning

8 Program requirements 8 Barry Boehms example from TRW Initial requirement: sub-second response time Highly complex design Estimate cost: $100 million Further analysis: Users happy with four-second response time 90% of the time Reduced system cost by ~$70 million Make sure youre solving a problem that needs to be solved

9 Program design 9 Set overall system goal Then set subsystems, features, classes Helps to identify troublesome subsystems Making the objectives explicit improves the likelihood theyll be achieved

10 Code Tuning 10 Not the most effective Not the easiest Not always the cheapest So…. why do it? Personal satisfaction? Rite of passage?

11 Pareto Principle 11 Also known as the 80/20 rule 80 percent of the result is achieved by 20 percent of the effort An Empirical Study of Fortran Programs by Donald Knuth < 4% of a program accounts for more than 50% of its run-time How does this relate to code tuning?

12 Lets consider… 12 Reducing the lines of code in a high-level language improves speed or the size of the resulting machine code

13 Which is faster? 13 for i = 1 to 10 a[ i ] = i end for 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

14 Results… 14 Language for-Loop Time Straight-code time Time Savings Performance Ratio Visual Basic8.473.1663%2.5:1 Java12.63.2374%4:1

15 Common sources of inefficiencies 15 I/O Operations Paging System Calls Interpreted languages Errors

16 16 Code Tuning Techniques

17 CotD 17

18 Logic 18 Stop testing when you know the answer Short circuit in conditionals and loops Standard with C++ and Java Order tests by frequency Compare performance of similar logic structures Some languages case is faster than if-then-else Substitute table lookups for complicated expressions

19 Loops - unswitching 19 Make decisions outside of the loop when possible ie. Putting the loop inside the conditional Good for ~20% time savings for ( i = 0; i < count; i++ ) { if ( sumType == SUMTYPE_NET) { netSum = netSum + amount[ i ]; } else{ grossSum = grossSum + amount[ i ]; } if ( sumType == SUMTYPE_NET) { for ( i = 0; i < count; i++ ) { netSum = netSum + amount[ i ]; } }else{ for ( i = 0; i < count; i++ ) { grossSum = grossSum + amount[ i ]; }

20 More loops 20 Jamming Combine loops that operate over the same elements Unrolling Reduce the amount of loop housekeeping Minimize work inside of the loop Assign complicated pointer expressions to a well-named variable

21 More loops cont. 21 Sentinel values Simplifying compound tests Nested loops: Put busiest loop on the inside Strength reduction Replace expensive operations (multiplication) with cheaper operations (addition)

22 Data transformations 22 Use integers rather than floating-point numbers Use the fewest array dimensions possible Minimize array references Use supplementary indexes Use caching

23 Expressions 23 Exploit algebraic identities not a and not b vs. not (a or b) Strength reduction Initialize at compile time More in the book…

24 Read the book for… 24 Routines Recoding in a low-level language Checklists Additional resources

25 25 Questions?


Download ppt "Tori Bowman CSSE 375, Rose-Hulman October 22, 2007 1 Code Tuning (Chapters 25-26 of Code Complete)"

Similar presentations


Ads by Google