Chapter 25: Code-Tuning Strategies. Chapter 25 Code tuning is one way of improving a program’s...

Preview:

Citation preview

Chapter 25: Code-Tuning Strategies

Chapter 25

Code tuning is one way of improving a program’s performance, You can often find other ways to improve performance more-and in less time and with less harm to the code-than by code tuning.

Performance and Code Tuning Efficiency as a priority

Program requirements Program design Class and routine design Operating-system interactions Code compilation Hardware Code tuning

Program Requirements

Performance is stated as priority more often than it actually is a requirement

Before you invest time solving a performance problem, make sure that you’re solving a problem that needs to be solved.

Program Design

Program design includes the major strokes of the design for a single program, mainly the way in which a program is divided into classes.

Some program designs make it difficult to write a high-performance system.

Class and Routine Design

Designing the internals of classes and routines presents another opportunity to design for performance.

The choice of data types and algorithms is a performance factor that can effect the program’s memory and execution speed.

Code Compilation

Compilers turn clear, high-level language code into optimized machine code.

If you choose the right compiler, you might not need to think about optimizing speed any further.

Hardware

Sometimes the cheapest and best way to improve a program’s performance is to buy new hardware.

If you’re developing custom software for a few in-house users, a hardware upgrade might be the cheapest option.

Code Tuning

Code tuning is the practice of modifying correct code in ways that make ir 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.

The Pareto Principle

The Pareto Principle states that you can get 80 percent of the result with 20 percent of the effort.

Jon Bentley describes a case in which a 1000 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.

Old Wives’ Tales

Reducing the lines of code in a high-level language improves the speed or size of the resulting machine code.

Examples: For i=1to 10

a[i] = IEnd for A[1] = 1 A[2]=2 ….

Old Wives’ Tales

Certain operations are probably faster or smaller than others.

You should optimize as you go. A fast program is just as important

as a correct one.

Compiler Optimizations

Shop for a compiler because each compiler can add performance to certain parts of your code.

Common Sources of operations Input/output operations An operation that causes the

operating system to swap pages of memory is much slower than an operation that works on only one page of memory

System calls can eat up a lot of time. Avoid going to the system

Chapter 26: Code-Tuning Techniques

Stop Testing When You Know the Answer negativeInputFound=false; For (…..){ if(input[i]<0){ negativeInputFound=true; } }

Order Tests by Frequency

Arrange test so that the one that’s fastest and most likely to be true is performed first.

Switch statement.

Compare Performance of Similar Logic Structures Switch statement vs. if statement

VS.

Use Lazy Evaluation

Some things can wait to be calculated.

Jamming

Jamming is the result of combining two loops that operate on the same set of elements.

Unrolling

Often more lines of code can be more efficient.

Minimizing the Work inside loops Improving readability Saving pointers as a well named

variable.

Putting the Busiest Loop on the inside When you have nested loops it is

better to put the bigger loop on the inside.

It can save time up to 33% in C++

Data transformations

Use integers rather than floating –point numbers.

Addition and multiplication of integers is faster then floating point values

Arrays

Use the fewest array dimensions as possible

Minimize Array references

Recommended