6.4 Optimization levels
In order to control compilation-time and compiler memory usage, and the
trade-offs between speed and space for the resulting executable, GCC
provides a range of general optimization levels, numbered from 0--3, as
well as individual options for specific types of optimization.
An optimization level is chosen with the command line option
LEVEL is a number from 0 to 3.
The effects of the different optimization levels are described below:
-O0 or no
-O option (default)
At this optimization level GCC does not perform any optimization and
compiles the source code in the most straightforward way possible. Each
command in the source code is converted directly to the corresponding
instructions in the executable file, without rearrangement. This is the
best option to use when debugging a program and is the default
if no optimization level option is specified.
This level turns on the most common forms of optimization that do not
require any speed-space tradeoffs. With this option the resulting
executables should be smaller and faster than with
more expensive optimizations, such as instruction scheduling, are not
used at this level.
Compiling with the option
-O1 can often take less time than
-O0, due to the reduced amounts of data that
need to be processed after simple optimizations.
This option turns on further optimizations, in addition to those used by
-O1. These additional optimizations include instruction
scheduling. Only optimizations that do not require any speed-space
tradeoffs are used, so the executable should not increase in size. The
compiler will take longer to compile programs and require more memory
-O1. This option is generally the best choice for
deployment of a program, because it provides maximum optimization
without increasing the executable size. It is the default optimization
level for releases of GNU packages.
This option turns on more expensive optimizations, such as function
inlining, in addition to all the optimizations of the lower levels
-O3 optimization level may
increase the speed of the resulting executable, but can also increase
its size. Under some circumstances where these optimizations are not
favorable, this option might actually make a program slower.
This option turns on loop-unrolling, and is independent of the other
optimization options. It will increase the size of an executable.
Whether or not this option produces a beneficial result has to be examined
on a case-by-case basis.
This option selects optimizations which reduce the size of an
executable. The aim of this option is to produce the smallest possible
executable, for systems constrained by memory or disk space. In some
cases a smaller executable will also run faster, due to better cache
It is important to remember that the benefit of optimization at the
highest levels must be weighed against the cost. The cost of
optimization includes greater complexity in debugging, and increased
time and memory requirements during compilation. For most purposes it
is satisfactory to use
-O0 for debugging, and
development and deployment.