Next: , Previous: Controlling Run-Time Checks, Up: Performance Considerations


25.2 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, on the gcc command line to control the optimization level:

-O0
no optimization (the default)
-O1
medium level optimization
-O2
full optimization
-O3
full optimization, and also attempt 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.

Note: 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 see Inlining of Subprograms.