1 / 25

Code Tuning (Chapters 25-26 of Code Complete)

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:

sophiat
Download Presentation

Code Tuning (Chapters 25-26 of Code Complete)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. 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

  4. Code Tuning Strategies

  5. 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

  6. 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

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

  8. 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

  9. 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

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

  11. 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?

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

  13. 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

  14. Results…

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

  16. Code Tuning Techniques

  17. CotD

  18. 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

  19. 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 ]; } }

  20. 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

  21. 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)

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

  23. Expressions • 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… • Routines • Recoding in a low-level language • Checklists • Additional resources

  25. Questions?

More Related