chapter 25: code-tuning strategies. chapter 25 code tuning is one way of improving a program’s...

26
Chapter 25: Code-Tuning Strategies

Upload: lesley-morgan

Post on 03-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

Chapter 25: Code-Tuning Strategies

Page 2: 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

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.

Page 3: 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 and Code Tuning Efficiency as a priority

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

Page 4: 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

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.

Page 5: 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

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.

Page 6: 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

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.

Page 7: 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

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.

Page 8: 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

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.

Page 9: 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

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.

Page 10: 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

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.

Page 11: 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

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

Page 12: 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

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.

Page 13: 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

Compiler Optimizations

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

Page 14: 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

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

Page 15: 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

Chapter 26: Code-Tuning Techniques

Page 16: 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

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

Page 17: 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

Order Tests by Frequency

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

Switch statement.

Page 18: 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

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

VS.

Page 19: 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

Use Lazy Evaluation

Some things can wait to be calculated.

Page 20: 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

Jamming

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

Page 21: 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

Unrolling

Often more lines of code can be more efficient.

Page 22: 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

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

variable.

Page 23: 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

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++

Page 24: 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

Data transformations

Use integers rather than floating –point numbers.

Addition and multiplication of integers is faster then floating point values

Page 25: 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

Arrays

Use the fewest array dimensions as possible

Minimize Array references

Page 26: 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