Performance Tuning Techinques.
1. Stop testing when you know the answer.
Some languages provides a form of expression evaluation known as "short-circuit evaluation", which means that the compiler generates code that automatically stops testing as soon as it knows the answer. If your language doesn't support short-circuit evaluation natively, you have to aovid using 'and' and 'or'.
The principle is a good one for many other kinds of cases as well. A search loop is a common case. For example, you're scanning an array of input numbers to find out a negative value.
a. Add a break statement after the negative value is found.
b. If your language doesn't have break, emulate a break with a goto that goes to the first statement after the loop.
c. Change the for loop to a while loop.
2. Order tests by frequency.
Arrange tests so that the one that's fastest and most likely to be true is performed first. This principle applies to case statements and to chains of if-then-elses.
3. Compare performance of similar logic structures.
Language case if-then-else Performance Ration
C# 0.260 0.330 1:1
Java 2.56 0.450 6:1
Visual Basic 0.260 1.00 1:4
4. Substitute table lookups for complicated
In some circumstances, a table lookup may be quicker than traversing a complicated chain of logic. The detailed informaction please refer to "Table-Driven Methods"
5. Use lazy evaluation.
Lazy evaluation is similar to just-in-time strategies that do the work closest to when it's needed.
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.
Jamming, or "fusion", is the result of combining two loops that operate on the same set of elements.
Loop jamming has two main hazards. First the indexes for the two parts that have been jammed might change so that they are no longer compatible. Second, you might not be able to combine the loops easily.
The goal of loop unrolling is to reduce the amount of loop housekeeping. Although completely unrolling a loop is a fast solution and works well when you're dealing with a small number of elements, it's not practical when you have a large number of elements or when you don't know in advance how many elements you'll have.
4. Minimizing the work inside loops
One key to writing effective loops is to minimize the work done inside a loop.
5. Sentinel values.
When you have a loop with a compound test, you can often save time by simplifying the test. If the loop is a search loop, one way to simplify the test is to use a sentinel value, a value that you put just past the end of the search range and that's guaranteed to terminate the search.
6. Putting the busiest loop on the inside.
When you have nested loops, think about which loop you want on the outside and which you want on the inside.
7. Strength reduction.
Reducing strength means replaceing an expensive operation such as multiplication with a cheaper operation such as addition.
1. Use integers rather than floating-point numbjers.
2. Use the fewest array dimensions possible.
3. Minimize array references.
4. Use supplementary indexes.
5. Use caching.
1. Exploit algebraic identities, for example: not a and not b=not (a or b), but if you choose the second one instead of the first one, you can save a not operation.
2. Use strength reduction.
a. Replace multiplication with addition.
b. Replace exponentiation with multiplication.
c. Replace trigonometric routines with their trigonometric identities.
d. Replace longlong integers with longs or ints.
e. Replace floating-point numbers with fixed-point numbers or integers.
f. Replace double-precision floating with single-precision numbers.
g. Replace integer multiplication-by-two and division-by-two with shift operations.
3. Initialize at complie time.
4. Be wary of system routines.
5. Use the correct types of constants.
6. Precompute results.
If the results are used many times, it's often cheaper to compute them once and look them up the rest of the time.
7. Eliminate common subexpressions.
If you find a expression that's repeated several tiems, assign it to a variable and refer to the variable rather than recomputing the expression in several places.