2.7. 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.

$ leon-coff-gcc -c -O0 whetstone.adb
$ leon-coff-size whetstone.o
   text    data     bss     dec     hex filename
  22312       0       0   22312    5728 whetstone.o
$ leon-coff-gnatmake -f -O0 whetstone
$ leon-coff-run whetstone
,.,. Whetstone GTS Version 0.1
---- Floating point benchmark.
Time taken =         325 mSec
Whetstone rating = 3077 KWIPS

Here are the results when compiling with optimization level 2.

$ leon-coff-gcc -c -O2 whetstone.adb
$ leon-coff-size whetstone.o
   text    data     bss     dec     hex filename
  10976       0       0   10976    2ae0 whetstone.o
$ leon-coff-gnatmake -f -O2 whetstone
$ leon-coff-run whetstone
,.,. Whetstone GTS Version 0.1
---- Floating point benchmark.
Time taken =         184 mSec
Whetstone rating = 5431 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.