Performance Tuning Techniques.

xiaoxiao2021-03-06  106

Logic1. 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 eviTi, 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 does not 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 RationC # 0.260 0.330 1: 1Java 2.56 0.450 6:. 1Visual Basic 0.260 1.00 1:44 Substitute table lookups for complicatedIn 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 evataration.lazy evAluation is Similar To Just-in-Time Strategies That Do The Work Closest to When It's NEEDED.

Loops1. UnswitchingSwitching refers to making a decision inside a loop every time it's executed. If the decision does not 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.2. JammingJamming, 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 easily.3 loops. Unrolling.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 do not know in advance how many elements y ou'll have.4. Minimizing the work inside loopsOne 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 sampleinel value, a value trory you put just paste the end of the search range and try

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.Data Transformations1. 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 .

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 strongth reduuction. 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 V Ariable Rather Than Recomputing The Expression in Several Places.

转载请注明原文地址:https://www.9cbs.com/read-102392.html

New Post(0)