GCC compiler optimization options
On a recent project, I had to meet specific timing requirements. One piece of code had to perform some complex processing on a frequent bases and could not be late without causing problems on the output that would be noticed. Once I got the initial version of the thread done, but before I had done any code tweaking to help reduce execution time, I changed the compiler optimization option.
Optimized with -O0. Processor was not able to keep up with required speed. Program executed at about 65% of the required speed.
Optimized with -O1 program was able to run at the required speed and processor utilization was down to about 62%. So with NO SOURCE CODE CHANGES, the program went from not being able to meet the timing requirement by about 35%, to being able to meet the requirements and still have about 38% processor headroom.
Optimized with -O2 program was able to run at the required speed and processor utilization was down to about 48%. So with once again with NO SOURCE CODE CAHNGES, the program went from not being able to meet the requirement by about 35%, to being able to meet the requirements and still have about 52% processor headroom.
GCC compiler optimization options.
In this article we are going to talk briefly about only three of these options.
The first option is optimization level 0. This option is selected with the command line option -O0. When debugging this is the option you will want to use. It tells the compiler to skip all optimizations, compile quickly and produce code that does not cause problems when debugging with GDB.
The second option is optimization level 1. This option is selected with the command line option -O1. This optimization level tells the compiler to perform the optimizations that will provide the greatest gains in application execution speed, while keeping compile time down to a reasonable level. You will not want to use this option while debugging because the compiler will make changes to your code that the debugger will not be able to correlate to your source files. It may do things like remove variables that do not seem to be used, rearrange code for faster execution, or even remove redundant / unreachable code. When using this option the compiler will do things like removing those FOR Loops that you use for delays.
The final option is optimization level 2. This option is selected with the command line option -O2. This option tells the compiler perform all optimizations from -O1 plus additional optimizations that will take more time to compile and may not produce much faster object code.