Next: Debugging Optimized Code, Previous: Use of Restrictions, Up: Performance Considerations
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 -O switch (the permitted forms are -O0, -O1 -O2, -O3, and -Os) to gcc to control the optimization level:
Note that many other compilers do fairly extensive optimization
even if "no optimization" is specified. When using gcc, it is
very unusual to use -O0 for production if
execution time is of any concern, since -O0
really does mean no optimization at all. This difference between
gcc and other compilers should be kept in mind when doing
performance comparisons.
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.
The -Os switch is independent of the time optimizations, so you can specify both -Os and a time optimization on the same compile command.
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.