Taking Advantage of the Compiler's Optimization Ability

OPTIMIZE is a directory of C++ programs which demonstrate the use and benefits of the compiler's automatic optimization ability.

Most compilers have an option to "optimize" your code; that is, to spend some extra compilation time looking for ways to adjust your program so that it runs faster, while getting the same results.

By default, compiler optimization is not accessed. In GFORTRAN, you can request optimization by including the "-O#' option where '#' is a value between 0 (no optimization) and 3 (high optimization). Asking for a higher level of optimization may increase the compilation time somewhat, and occasionally, an optimization performed by the compiler might not be suitable for your program (in other words, it might make your program return different or incorrect results.) Thus, it's important to time your code before and after optimization, to see if it helps, and to check your results, to see if it hurts!

The molecular dynamics example program given here runs in about 72 seconds. After optimization at level 1, it runs in 50 seconds. Optimization level 2 brings the run time down to 48 seconds and level 3 to 54 seconds. These improvements come with not a single change to the source code.


The computer code and data files described and made available on this web page are distributed under the GNU LGPL license.


OPTIMIZE is available in a C version and a C++ version and a FORTRAN90 version.

Related Data and Programs:

GPROF, C++ programs which illustrate the use of the GPROF program performance monitor;

MD, a C++ program which carries out a molecular dynamics simulation, intended as a starting point for implementing a parallel version.

TIMER, C++ programs which demonstrate how to compute CPU time or elapsed time.

Examples and Tests:

MD is a molecular dynamics program.

You can go up one level to the C++ source codes.

Last revised on 13 March 2012