260 likes | 303 Views
Code Tuning (Chapters 25-26 of Code Complete). Tori Bowman CSSE 375, Rose-Hulman October 22, 2007. Outline. State of 375 This Code Tuning Strategies Code Tuning Techniques. State of 375. Tonight HW 5 due 11:55 pm Tomorrow: part lecture/part work time Thursday:
E N D
Code Tuning(Chapters 25-26 of Code Complete) Tori Bowman CSSE 375, Rose-Hulman October 22, 2007
Outline • State of 375 • This • Code Tuning Strategies • Code Tuning Techniques
State of 375 • Tonight • HW 5 due 11:55 pm • Tomorrow: part lecture/part work time • Thursday: • Week one progress report • Friday: • In class work time
Overview • Code-tuning is one method of improving a program’s performance through various techniques • Performance is loosely related to speed • When you work on speed, you’re not working on other quality attributes
Historical context • 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
Efficiency viewpoints • Program requirements • Program design • Class and routine design • Operating-system interactions • Code compilation • Hardware • Code tuning
Program requirements • Barry Boehm’s example from TRW • Initial requirement: sub-second response time • Highly complex design • Estimate cost: $100 million • Further analysis: • User’s happy with four-second response time 90% of the time • Reduced system cost by ~$70 million • Make sure you’re solving a problem that needs to be solved
Program design • Set overall system goal • Then set subsystems, features, classes • Helps to identify troublesome subsystems • Making the objectives explicit improves the likelihood they’ll be achieved
Code Tuning • Not the most effective • Not the easiest • Not always the cheapest • So…. why do it? • Personal satisfaction? • Rite of passage?
Pareto Principle • 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 it’s run-time • How does this relate to code tuning?
Let’s consider… Reducing the lines of code in a high-level language improves speed or the size of the resulting machine code
Which is faster? 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
Common sources of inefficiencies • I/O Operations • Paging • System Calls • Interpreted languages • Errors
Logic • 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
Loops - unswitching • 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 ]; } }
More loops • 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
More loops cont. • Sentinel values • Simplifying compound tests • Nested loops: • Put busiest loop on the inside • Strength reduction • Replace expensive operations (multiplication) with cheaper operations (addition)
Data transformations • Use integers rather than floating-point numbers • Use the fewest array dimensions possible • Minimize array references • Use supplementary indexes • Use caching
Expressions • Exploit algebraic identities • not a and not b vs. not (a or b) • Strength reduction • Initialize at compile time • More in the book…
Read the book for… • Routines • Recoding in a low-level language • Checklists • Additional resources