tori bowman csse 375, rose-hulman october 22, 2007 1 code tuning (chapters 25-26 of code complete)
TRANSCRIPT
Tori Bowman
CSSE 375, Rose-Hulman
October 22, 2007
1
Code Tuning(Chapters 25-26 of
Code Complete)
Outline
2
State of 375This
Code Tuning StrategiesCode Tuning Techniques
State of 375
3
TonightHW 5 due 11:55 pm
Tomorrow: part lecture/part work timeThursday:
Week one progress reportFriday:
In class work time
4
Code Tuning Strategies
Overview
5
Code-tuning is one method of improving a program’s performance through various techniques
Performance is loosely related to speedWhen you work on speed, you’re not working
on other quality attributes
Historical context
6
1960s:Resources severely limitedNothing more important than efficiency
1970s:Computing improvesRealizing how much it hurt readability and
maintainability1980s:
Microcomputers brought efficiency issues backWaned in throughout the 1990s
2000s:Memory in embedded issues becomes the
efficiency concern
Efficiency viewpoints
7
Program requirementsProgram designClass and routine designOperating-system interactionsCode compilationHardwareCode tuning
Program requirements
8
Barry Boehm’s example from TRWInitial requirement: sub-second response
timeHighly complex designEstimate cost: $100 million
Further analysis:User’s happy with four-second response time
90% of the timeReduced system cost by ~$70 million
Make sure you’re solving a problem that needs to be solved
Program design
9
Set overall system goalThen set subsystems, features, classes
Helps to identify troublesome subsystemsMaking the objectives explicit improves the
likelihood they’ll be achieved
Code Tuning
10
Not the most effectiveNot the easiestNot always the cheapest
So…. why do it?Personal satisfaction?Rite of passage?
Pareto Principle
11
Also known as the 80/20 rule80 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…
12
Reducing the lines of code in a high-level language improves speed or the size of the resulting machine code
Which is faster?
13
for i = 1 to 10a[ i ] = i
end for
a [ 1 ] = 1a [ 2 ] = 2a [ 3 ] = 3a [ 4 ] = 4a [ 5 ] = 5a [ 6 ] = 6a [ 7 ] = 7a [ 8 ] = 8a [ 9 ] = 9a [ 10 ] = 10
Results…
14
Languagefor-Loop
TimeStraight-code time
Time Savings
Performance Ratio
Visual Basic 8.47 3.16 63% 2.5:1
Java 12.6 3.23 74% 4:1
Common sources of inefficiencies
15
I/O OperationsPagingSystem CallsInterpreted languagesErrors
16
Code Tuning Techniques
CotD
17
Logic
18
Stop testing when you know the answerShort circuit in conditionals and loopsStandard with C++ and Java
Order tests by frequencyCompare performance of similar logic
structuresSome languages case is faster than if-then-else
Substitute table lookups for complicated expressions
Loops - unswitching
19
Make decisions outside of the loop when possibleie. Putting the loop inside the conditionalGood 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
20
JammingCombine loops that operate over the same
elementsUnrolling
Reduce the amount of loop housekeepingMinimize work inside of the loop
Assign complicated pointer expressions to a well-named variable
More loops cont.
21
Sentinel valuesSimplifying compound tests
Nested loops:Put busiest loop on the inside
Strength reductionReplace expensive operations (multiplication)
with cheaper operations (addition)
Data transformations
22
Use integers rather than floating-point numbers
Use the fewest array dimensions possibleMinimize array referencesUse supplementary indexesUse caching
Expressions
23
Exploit algebraic identitiesnot a and not b vs. not (a or b)
Strength reductionInitialize at compile time
More in the book…
Read the book for…
24
RoutinesRecoding in a low-level languageChecklistsAdditional resources
25
Questions?