Optimization is the art of going through a program and making the code more efficient so that it runs faster. Most compilers have a command-line switch that causes them to generate optimized code. This efficiency comes at the cost of compile time; the compiler takes a lot longer to generate code when optimization is turned on.
The other type of optimization occurs when a programmer modifies a program to use a more efficient algorithm. This section discusses this second type of optimization.
How to Optimize:
Here are several optimizing strategies mentioned:
Loop ordering:
Nested loops should be ordered with the simplest loop outermost and the
most complex loops innermost.
Reduction in strength:
This phrase is a fancy way of saying you should use cheap operations instead
of expensive ones. The table at the top-right hand side explains the relative cost of each operation in a descending order.
Powers of 2:
Use a power of 2 when doing integer multiply or divide. Most compilers
substitute a shift for the operation.
Pointers:
Using pointers is faster than indexing an array. Pointers are, however, more
tricky to use.
Macros:
Using a macro eliminates the overhead associated with a function call. It also
makes the code bigger and a little more difficult to debug.
No comments:
Post a Comment