News

Nov 29, 2012
Chibios Essentials article added
Nov 6, 2012
 
ATL123 Embedded Systems

Blog

Nov 13, 2012 Posted by: jscott

GCC has many Optimization options.  Choosing wisely at different phases of your development cycle will help make the test/debug phase of your project easier and allow you to ship a smaller, faster program.
 

Comments??

 Add a Review of this item 
Comment Title:
Your Name:
Your Email Address:
Additional Comments:
 
This is a captcha-picture. It is used to prevent mass-access by robots. (see: www.captcha.net)
 
 

Arduino Uno Development Board

GCC compiler optimization options


Nov 13, 2012 Posted by: jscott

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.