Docsity

Code Tuning Techniques, Slides of Computer Science

Partial preview of the text

Download Code Tuning Techniques and more Slides Computer Science in PDF only on Docsity!

Code Tuning

Code Tuning

Code tuning is one way of improving a program’s performance.

It is the practice of modifying correct code in ways that make it run more

efficiently.

Tuning refers to small-scale changes that affect a single class, a single

routine, or, more commonly, a few lines of code.

Tuning does not refer to large-scale design changes, or other higher-level

means of improving performance.

Introduction to Code Tuning

Code tuning is appealing for several reasons

It’s incredibly satisfying to take a routine that executes in 20 microseconds,

tweak a few lines, and reduce the execution speed to 2 microseconds.

Appealing for programmers.

The problem with code tuning is that efficient code isn’t necessarily

“better” code.

The Pareto Principle

Also known as the 80/20 rule

You can get 80 percent of the result with 20 percent of the effort.

The principle applies to a lot of areas other than programming, but it

definitely applies to program optimization.

Barry Boehm reports that 20 percent of a program’s routines

consume 80 percent of its execution time

Introduction to Code Tuning

Jon Bentley describes a case in which a thousand-line program spent

80 percent of its time in a five-line square-root routine. By tripling

the speed of the square root routine, he doubled the speed of the

program

Code Tuning

Here are some common misapprehensions about code tuning

  • Reducing the lines of code in a high-level language improves the speed or

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

Code Tuning

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

Code Tuning

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

Code-Tuning Techniques

1. Logic

Much of programming consists of manipulating logic. This technique

describes how to manipulate logical expressions to your advantage.

Stop Testing When You Know the Answer

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.

Code-Tuning Techniques

C++ Example of Not Stopping After You Know the Answer

Code-Tuning Techniques

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.

Code-Tuning Techniques

Use Lazy Evaluation

Idea: wait to compute until you’re sure you need the value

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.

Code-Tuning Techniques

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:

Code-Tuning Techniques

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: