导航

Rules for Code Tuning (From Programming Pearls)

Posted on 2006-06-22 21:47  InterMa  阅读(640)  评论(0编辑  收藏  举报

这片小文是《Programming Pearls》的一篇附录,我从中选取了几条比较实用的摘录如下,全文在:
http://www.cs.bell-labs.com/cm/cs/pearls/apprules.html

Space-For-Time Rules(空间换时间):

Data Structure Augmentation. The time required for common operations on data can often be reduced by augmenting the structure with extra information or by changing the information within the structure so that it can be accessed more easily.

Store Precomputed Results. The cost of recomputing an expensive function can be reduced by computing the function only once and storing the results. Subsequent requests for the function are then handled by table lookup rather than by computing the function.

Caching. Data that is accessed most often should be the cheapest to access.

Lazy Evaluation. The strategy of never evaluating an item until it is needed avoids evaluations of unnecessary items.

Time-For-Space Rules(时间换空间):

Packing. Dense storage representations can decrease storage costs by increasing the time required to store and retrieve data.

Interpreters. The space required to represent a program can often be decreased by the use of interpreters in which common sequences of operations are represented compactly.

Loop Rules(循环规则):

Code Motion Out of Loops. Instead of performing a certain computation in each iteration of a loop, it is better to perform it only once, outside the loop.

Combining Tests. An efficient inner loop should contain as few tests as possible, and preferably only one. The programmer should therefore try to simulate some of the exit conditions of the loop by other exit conditions.

Loop Fusion. If two nearby loops operate on the same set of elements, then combine their operational parts and use only one set of loop control operations.

Logic Rules(逻辑规则):

Reordering Tests. Logical tests should be arranged such that inexpensive and often successful tests precede expensive and rarely successful tests.

Precompute Logical Functions. A logical function over a small finite domain can be replaced by a lookup in a table that represents the domain.

Short-Circuiting Monotone Functions. If we wish to test whether some monotone nondecreasing function of several variables is over a certain threshold, then we need not evaluate any of the variables once the threshold has been reached.

Procedure Rules(过程规则):

Exploit Common Cases. Functions should be organized to handle all cases correctly and common cases efficiently.

Coroutines. A multiple-pass algorithm can often be turned into a single-pass algorithm by use of coroutines.

Transformations on Recursive Functions.Rewrite recursion to iteration.

Parallelism. A program should be structured to exploit as much of the parallelism as possible in the underlying hardware.

Expression Rules(表达式规则):

Compile-Time Initialization. As many variables as possible should be initialized before program execution.

Common Subexpression Elimination. If the same expression is evaluated twice with none of its variables altered between evaluations, then the second evaluation can be avoided by storing the result of the first and using that in place of the second.

Pairing Computation. If two similar expressions are frequently evaluated together, then we should make a new procedure that evaluates them as a pair.

Exploit Word Parallelism. Use the full data-path width of the underlying computer architecture to evaluate expensive expressions.