# Optimization

• There is a general switch ‘-O<optimization_level>’ which specifies the level of optimization used when generating the code:

• -Os

Signal that the generated code should be optimized for code size. The compiler will not care about the execution performance of the generated code.

• -O0

No optimization. GCC will generate code that is easy to debug but slower and larger than with the incremental optimization levels outlined below.

• -O1 or -O

This will optimize the code for both speed and size. Most statements will be executed in the same order as in the C/C++ code and most variables can be found in the generated code. This makes the code quite suitable for debugging. This is default.

• -O2

Turn on most optimizations in GCC except for some optimizations that might drastically increase code size. This also enables instruction scheduling, which allows instructions to be shuffled around to minimize CPU stall cycles because of data hazards and dependencies, for CPU architectures that might benefit from this. Overall this option makes the code quite small and fast, but hard to debug.

• -O3

Turn on some extra performance optimizations that might drastically increase code size but increase performance compared to the -O2 and -O1 optimization levels. This includes performing function inlining

• Other optimization options

• -ffunction-sections

• -fdata-sections

Place each function or data item into its own section in the output file if the target supports arbitrary sections. The name of the function or the name of the data item determines the section's name in the output file.

Only use these options when there are significant benefits from doing so. When you specify these options, the assembler and linker will create larger object and executable files and will also be slower.

• -funroll-loops

Perform loop unrolling when iteration count is known. If code size is not a concern then some extra performance might be obtained by making gcc unroll loops by using the ‘-funroll-loops’’ switch in addition to the ‘-O3’ switch.