Writing Efficient Programs

Writing Efficient Programs
by
Jon Louis Bentley

Summary of the Rules

Taken from Appendix C.


Space-For-Time Rules

Space-For-Time Rule 1---Data Structure Augmentation: The time required for a common operation 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.

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.

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

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


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.

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.


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.

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.

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

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.

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.

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.


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.

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.

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

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

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


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 by reduced by rewriting procedures in line and binding the passed variables.

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

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

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

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


Expression Rules

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

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.

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.

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.

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


Fundamental Rules

Fundamental Rule 1---Code Simplification: Most fast programs are simple. Therefore, keep code simple to make it faster.

Fundamental Rule 2---Problem Simplification: To increase the efficiency of a program, simplify the problem it solves.

Fundamental Rule 3---Relentless Suspicion: Question the necessity of each instruction in a time-critical piece of code and each field in a space-critical data structure.

Fundamental Rule 4---Early Binding: Move work forward in time. Specifically, do work now just once in hope of avoiding doing it many times later.


[ Miscellaneous | Krishna Kunchithapadam ]


Last updated: Sun Jun 27 17:00:19 PDT 2004
URL: http://geocities.datacellar.net/krishna_kunchith/misc/wep.html

1