






























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!































Code tuning is appealing for several reasons
You can get 80 percent of the result with 20 percent of the effort.
Here are some common misapprehensions about code tuning
size of the resulting machine code—false!
Consider the following code that initializes a 10-element array:
for i = 1 to 10
a[ i ] = i
end for
If you follow the old “fewer lines are faster” belief, you’ll guess that the first
code is faster because it has four fewer lines.
Tests in Visual Basic and Java have shown that the second fragment is at least 60
percent faster than the first. Here are the numbers
Certain operations are probably faster or smaller than others—false!
You should optimize as you go—false!
One theory is that if you strive to write the fastest and smallest possible code as
you write each routine, your program will be fast and small. This approach
creates a forest-for-the-trees situation in which programmers ignore significant
global optimizations because they’re too busy with micro-optimizations.
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
Suppose you have a statement like
if ( x< 5 ) and ( x < 10 ) then ...
Once you’ve determined that x is less than 5, you don’t need to perform the second half of the
test.
C++ Example of Not Stopping After You Know the Answer
Order Tests by Frequency
Arrange tests so that the one that’s fastest and most likely to be true is
performed first. It should be easy to drop through the normal case, and if there
are inefficiencies, they should be in processing the uncommon cases. This
principle applies to case statements and to chains of if-then-else s.
Use Lazy Evaluation
Often, you never actually use the value!
Example:
your program contains a table of 5000 values, generates the whole table at
startup time, and then uses it as the program executes. If the program uses only
a small percentage of the entries in the table, it might make more sense to
compute them as they’re needed rather than all at once. Once an entry is
computed, it can still be stored for future reference.
2. Loops
Because loops are executed many times, the hot spots in a program are often inside loops. The
techniques make the loop itself faster.
Un-switching
Switching refers to making a decision inside a loop every time it’s executed. If
the decision doesn’t change while the loop is executing, you can unswitch the
loop by making the decision outside the loop.
Usually this requires turning the loop inside out, putting loops inside the
conditional rather than putting the conditional inside the loop. Here’s an
example of a loop before unswitching:
C++ Example of a Switched Loop
In this code, the test if ( sumType == SUMTYPE_NET ) is repeated through each
iteration even though it’ll be the same each time through the loop. You can
rewrite the code for a speed gain this way: