Simple Examples for Timing C/C++ and Fortran Code Segments under Gnu Compilers

Andrew J. Pounds, Ph.D
Departments of Chemistry and Computer Science
Mercer University

Numerous websites contain examples of how one might do code timing under C or Fortran. Few websites, however, provide all of the requisite pieces one might need to start timing code. In these examples we demonstrate how one can write a simple wrapper functions to utilize Unix system calls, place those functions in a library (named lbstime.a), and then use the functions from the library in both C/C++ and Fortran to time code segments. Two commented driver programs (one in C an one in Fortran) are provided to demonstrate how to call the library functions. A makefile is also provided to build all of the components.


fdriver.f (Fortran program that utilizes library)
cdriver.c (C program that utilizes the library)
cputime.cc (double function to return cputime)
walltime.cc (double function to return walltime)
makefile (compiles everything and also builds the library)