SUMMARY OF THE RULES

The following list restates each rule from Chapters 4 and 5 and then briefly summarizes the major points made in the text. A list of the names of the rules can be found in Section 7.2 on page 110.

SPACE-FOR-TIME RULES

Space-For-Time Rule 1-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. (Page 39.)

Space-For-Time Rule 2-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. (Page 40.)

Space-For-Time Rule 3-Caching: Data that is accessed most often should be the cheapest to access. (Page 42.)

Space-For-Time Rule 4-Lazy Evaluation: The strategy of never evaluating an item until it is needed avoids evaluations of unnecessary items. (Page 43.)

TIME-FOR-SPACE RULES

Time-For-Space Rule 1-Packing: Dense storage representations can decrease storage costs by increasing the time required to store and retrieve data. (Page 45.)

Time-For-Space Rule 2-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. (Page 47.)

LOOP RULES

Loop Rule 1-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. (Page 52.)

Loop Rule 2-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. (Page 53.)

Loop Rule 3-Loop Unrolling: A large cost of some short loops is in modifying the loop indices. That cost can often be reduced by unrolling the loop. (Page 56.)

Loop Rule 4-Transfer-Driven Loop Unrolling: If a large cost of an inner loop is devoted to trivial assignments, then those assignments can often be removed by repeating the code and changing the use of variables. Specifically, to remove the assignment I: = J, the subsequent code must treat J as though it were I. (Page 59.)

Loop Rule 5-Unconditional Branch Removal: A fast loop should contain no unconditional branches. An unconditional branch at the end of a loop can be removed by "rotating" the loop to have a conditional branch at the bottom. (Page 62.)

Loop Rule 6-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. (Page 63.)

LOGIC RULES

Logic Rule 1-Exploit Algebraic Identities: If the evaluation of a logical expression is costly, replace it by an algebraically equivalent expression that is cheaper to evaluate. (Page 66.)

Logic Rule 2-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. (Page 67.)

Logic Rule 3-Reordering Tests: Logical tests should be arranged such that inexpensive and often successful tests precede expensive and rarely successful tests. (Page 69.)

Logic Rule 4-Precompute Logical Functions: A logical function over a small finite domain can be replaced by a lookup in a table that represents the domain. (Page 72.)

Logic Rule 5-Boolean Variable Elimination: We can remove boolean variables from a program by replacing the assignment to a boolean variable V by an if-then-else statement in which one branch represents the case that V is true and the other represents the case that V is false. (This generalizes to case statements and other logical control structures.) (Page 73.)

PROCEDURE RULES

Procedure Rule 1-Collapsing Procedure Hierarchies: The run times of the elements of a set of procedures that (nonrecursively) call themselves can often be reduced by rewriting procedures in line and binding the passed variables. (Page 75.)

Procedure Rule 2-Exploit Common Cases: Procedures should be organized to handle all cases correctly and common cases efficiently. (Page 76.)

Procedure Rule 3-Coroutines: A multiple-pass algorithm can often be turned into a single-pass algorithm by use of coroutines. (Page 79.)

Procedure Rule 4-Transformations on Recursive Procedures: The run time of recursive procedures can often be reduced by applying the following transformations: (Page 80.)

Procedure Rule 5-Parallelism: A program should be structured to exploit as much of the parallelism as possible in the underlying hardware. (Page 80.)

EXPRESSION RULES

Expression Rule 1-Compile-Time Initialization: As many variables as possible should be initialized before program execution. (Page 82.)

Expression Rule 2-Exploit Algebraic Identities: If the evaluation of an expression is costly, replace it by an algebraically equivalent expression that is cheaper to evaluate. (Page 82.)

Expression Rule 3-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. (Page 84.)

Expression Rule 4-Pairing Computation: If two similar expressions are frequently evaluated together, then we should make a new procedure that evaluates them as a pair. (Page 84.)

Expression Rule 5-Exploit Word Parallelism: Use the full word width of the underlying computer architecture to evaluate expensive expressions. (Page 85.)

From Writing Efficient Programs by Jon Bentley; Prentice-Hall 1982 ISBN 0-13-970244-X