|An Introduction to GCC - for the GNU compilers gcc and g++|
by Brian J. Gough, foreword by Richard M. Stallman
Paperback (6"x9"), 144 pages
RRP £12.95 ($19.95)
"A wonderfully thorough guide... well-written, seriously usable information" --- Linux User and Developer Magazine (Issue 40, June 2004) Get a printed copy>>>
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:
- 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
-O0. The more expensive optimizations, such as instruction scheduling, are not used at this level. Compiling with the option
-O1can often take less time than compiling with
-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 than with
-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
-O3optimization 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 usage.
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.
|ISBN 0954161793||An Introduction to GCC - for the GNU compilers gcc and g++||See the print edition|