2.6. Using Optimizations

Optimization makes your program smaller and faster. In most cases it also makes the generated code easier to understand. So think of the option -O2 as the norm, and only use other levels of optimization when you want to get something special.

The extent to which optimization makes a whole program smaller and faster depends on many things. In the case of hello.adb there will be little benefit since most of the code in the executable file is in the library functions, and these are already optimized.

The following example is more representative and shows the Whetstone benchmark program reduced to 49% of its size, and running nearly twice as fast. You can find Whetstone in the CD-ROM directory benchmarks/.

Here are the results when compiling with no optimization.

$ m1750-coff-gcc -c -O0 whetstone.adb
$ m1750-coff-size whetstone.o
   text    data     bss     dec     hex filename
  18424       0       0   18424    47f8 whetstone.o
$ m1750-coff-gnatmake -f -O0 whetstone
$ m1750-coff-run whetstone
,.,. Whetstone GTS Version 0.1
---- Floating point benchmark.
Time taken = 2215 mSec
Whetstone rating = 451 KWIPS

Here are the results when compiling with optimization level 2. This is the default.

$ m1750-coff-gcc -c -O2 whetstone.adb
$ m1750-coff-size whetstone.o
   text    data     bss     dec     hex filename
   9540       0       0    9540    2544 whetstone.o
$ m1750-coff-gnatmake -f -O2 whetstone
$ m1750-coff-run whetstone
,.,. Whetstone GTS Version 0.1
---- Floating point benchmark.
Time taken =     1211 mSec
Whetstone rating =         825 KWIPS

At optimization level 3, the compiler will automatically in-line calls of small functions. This may increase the size of the generated code, and the code will run faster. However the code motion due to inlining may make the generated code difficult to read and debug.