|
Compilers often "know" the machine architecture and processor better than the developer, performing as a matter of course such optimizations as dead code elimination, loop unrolling, branch optimization, and function inlining. In many cases, the programmer finds that all the major and obvious bottlenecks identified via profiling are resolved by using the optimizations provided by the compiler. In gcc, the general level of optimization is controlled by the O flag. At its most basic level, -O1, the flag takes the most general steps to reduce code size and execution time. O2 causes gcc to perform nearly all optimizations that do not involve a space-speed trade-off (such as loop unrolling or function inlining). O3 turns on such additional optimizations as function inlining and register renaming. We recommend an interative process of increasing optimization/profiling cycles to determine the best level for your application. This is because it is possible, even with well-written code, for a higher level of compiler aggressiveness in optimization to hurt a piece of code's performance rather than help it. Sometimes, this is a matter of what the more aggressive optimizer is looking for as opposed to what the code is actually trying to do. A reading of the performance options section of the gcc manual reveals many additional options, most of which are more suitable for use after a particular code section has been analyzed and its problems made clear. Basic Performance TuningFollowing are the basic steps in tuning a program:
Tuning your application for performance is a methodical and sometimes repetitive process that nonetheless can give great rewards in program efficiency. One Word About Compiler OptimizationsSometimes at the higher levels of compiler optimization, you may see a decrease in performance instead of an increase for a given module. Compilers are not perfect, and in these cases a closer examination of the code itself is warranted to see what can be done to "help" the compiler see what needs to be optimized through making the code simpler or using a different algorithm in that section. Following are some things to consider when developing code. Giving some consideration to your code can make all the difference. |
|