Next: , Previous: Use of Restrictions, Up: Performance Considerations


7.1.3 Optimization Levels

The default is optimization off. This results in the fastest compile times, but GNAT makes absolutely no attempt to optimize, and the generated programs are considerably larger and slower than when optimization is enabled. You can use the -On switch, where n is an integer from 0 to 3, to gcc to control the optimization level:

-O0
No optimization (the default); generates unoptimized code but has the fastest compilation time.
-O1
Medium level optimization; optimizes reasonably well but does not degrade compilation time significantly.
-O2
Full optimization; generates highly optimized code and has the slowest compilation time.
-O3
Full optimization as in -O2, and also attempts automatic inlining of small subprograms within a unit (see Inlining of Subprograms).

Higher optimization levels perform more global transformations on the program and apply more expensive analysis algorithms in order to generate faster and more compact code. The price in compilation time, and the resulting improvement in execution time, both depend on the particular application and the hardware environment. You should experiment to find the best level for your application.

Since the precise set of optimizations done at each level will vary from release to release (and sometime from target to target), it is best to think of the optimization settings in general terms. The Using GNU GCC manual contains details about the -O settings and a number of -f options that individually enable or disable specific optimizations.

Unlike some other compilation systems, gcc has been tested extensively at all optimization levels. There are some bugs which appear only with optimization turned on, but there have also been bugs which show up only in unoptimized code. Selecting a lower level of optimization does not improve the reliability of the code generator, which in practice is highly reliable at all optimization levels.

Note regarding the use of -O3: The use of this optimization level is generally discouraged with GNAT, since it often results in larger executables which run more slowly. See further discussion of this point in Inlining of Subprograms.