optimize_test, a C code which uses 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 65 seconds. After optimization at level 1, it runs in 33 seconds. Optimization level 2 brings the run time down to 33 seconds and level 3 to 33 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 MIT license


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

Related Data and Programs:

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

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

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

Source Code:

MD is a molecular dynamics program.

Last revised on 21 July 2019