2.2.4. Code optimization with the GCC
Compiler optimization transforms your code for efficiency. Use GCC options to balance compilation time with execution speed or binary size suitable for your deployment. Selecting the appropriate optimization level helps you achieve faster execution or smaller binaries for your application.
With GCC, you can set the optimization level by using the -Olevel option. This option accepts a set of values in place of the level.
| Level | Description |
|---|---|
|
| Optimize for compilation speed - no code optimization (default). |
|
| Optimize to increase code execution speed (the larger the number, the greater the speed). |
|
| Optimize for file size. |
|
|
Same as a level |
|
| Optimize for debugging experience. |
For release builds, use the optimization option -O2.
During development, the -Og option is useful for debugging the program or library in some situations. Because some bugs manifest only with certain optimization levels, test the program or library with the release optimization level.
GCC offers a large number of options to enable individual optimizations. For more information, see gcc man page for more details.