CHPC Software: Using Timing Routines

Built-in timing routines routines can measure wall-clock time, CPU time and system time. Subroutines exist so that C or Fortran routines can access wall-clock and CPU timing information. Both are discussed in this document.

Routines for getting the wall-clock, CPU time and system time exist both in the AIX operating system and the "csh" program. For instance, here is an example of the usage of the Unix timing command:

'>' time a.out
Real   1.0
User   0.7
System 0.0

More information on this command is available with the command "man time".

The timing command in the csh outputs the following:

'>' time a.out
0.7u 0.0s 1:00 70% 150+58k 0+0io 0pf+0w

More information on the csh timing routine is available using "man csh".

Timing routines can also be called from inside Fortran of C programs. Here are several timing routines you might consider adding to your files:

A C program to get the real time:

#include <sys/time.h>

int elapsed(double *sec)
{
  extern int gettimeofday();
  struct         timeval t;
  struct timezone tz;
  
  int stat;
  stat = gettimeofday(&t, &tz);
  *sec = (double)(t.tv_sec + t.tv_usec/1000000.0);
  return(stat);
}

A Fortran program that calls elapsed to get the CPU time:

program main
integer i, info
double precision dummy, begin, finish

integer elapsed
external elapsed

info = elapsed( begin )
do i = 1, 1000000
   dummy = dummy * 1000 + 2
enddo
info = elapsed( finish )
print *, finish - begin

end

A Fortran program to get CPU time in seconds.

double precision function dsecnd( )
integer t
integer mclock
external mclock

t = mclock( )
dsecnd = dble( t ) * 0.01D0
return
end

Fortran and C timing routines can be easily interchanged using interlanguage calls.

Last Modified: October 06, 2008 @ 21:07:47