next up previous index
Next: Timing a Job in Up: Timing a Job Previous: Timing a Job

Timing a Job in C

Probably most C-language programmers know how to time their jobs, because functions time and clock are parts of the standard C library, which is defined by ANSI C specifications.

Function time takes a pointer to time_t as an argument and returns a value of time_t on exit. On our system time_t is defined on /usr/include/sys/types.h and /usr/include/time.h as long. If the pointer is not NULL, the return value is also placed in whatever location the pointer points at. The returned value is the current calendar time, in seconds, since the Epoch, i.e., 00:00:00 GMT, 1st of January 1970: popularly celebrated as the day when UNIX was born.

You would use function time in order to find out about the elapsed wall-clock time. If you know that, say, your queue allows only up to two wall-clock hours (7200 seconds) per job, by checking how much time you've used so far, you will know how much time there is still left too.

Function clock does not take any arguments and returns a value ot type clock_t, which is defined on /usr/include/sys/types.h and /usr/include/time.h as int. This function returns CPU time that elapsed since the execution of the program commenced. The returned time is not in seconds. It is in clock cycles. There is a constant CLOCKS_PER_SEC defined on /usr/include/time.h, which tells how many clock cycles there are per second. So, in order to find out how many CPU seconds you have used so far you have to divide the result obtained by calling clock by CLOCKS_PER_SEC.

Because function clock returns clock_t, i.e., int (on AIX), it should be called frequently. Once the returned value reaches MAX_INT, which is defined on /usr/include/values.h, the clock resets itself and resumes counting from 0.

The following example illustrates how to use functions time and clock.

#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <stdio.h>
#include <math.h>

main()
{
  time_t  t0, t1; /* time_t is defined on <time.h> and <sys/types.h> as long */
  clock_t c0, c1; /* clock_t is defined on <time.h> and <sys/types.h> as int */

  long count;
  double a, b, c;

  printf ("using UNIX function time to measure wallclock time ... \n");
  printf ("using UNIX function clock to measure CPU time ... \n");

  t0 = time(NULL);
  c0 = clock();

  printf ("\tbegin (wall):            %ld\n", (long) t0);
  printf ("\tbegin (CPU):             %d\n", (int) c0);

  printf ("\t\tsleep for 5 seconds ... \n");
  sleep(5);

  printf ("\t\tperform some computation ... \n");
  for (count = 1l; count < 10000000l; count++) {
     a = sqrt(count);
     b = 1.0/a;
     c = b - a;
  }

  t1 = time(NULL);
  c1 = clock();

  printf ("\tend (wall):              %ld\n", (long) t1);
  printf ("\tend (CPU);               %d\n", (int) c1);
  printf ("\telapsed wall clock time: %ld\n", (long) (t1 - t0));
  printf ("\telapsed CPU time:        %f\n", (float) (c1 - c0)/CLOCKS_PER_SEC);
}
Compile this program with
gustav@sp20:../time 21:55:32 !861 $ gcc -o c-time c-time.c -lm
gustav@sp20:../time 21:55:40 !862 $
and run it as follows
gustav@sp20:../time 21:55:40 !862 $ time -p ./c-time
using UNIX function time to measure wallclock time ... 
using UNIX function clock to measure CPU time ... 
        begin (wall):            916801059
        begin (CPU):             0
                sleep for 5 seconds ... 
                perform some computation ... 
        end (wall):              916801070
        end (CPU);               5970000
        elapsed wall clock time: 11
        elapsed CPU time:        5.970000
Command exited with non-zero status 35
real 10.98
user 5.97
sys 0.01
gustav@sp20:../time 21:57:50 !863 $
Observe that times returned by this program agree with times returned by the GNU command time, which on our system lives in
/afs/ovpit.indiana.edu/@sys/gnu/bin


next up previous index
Next: Timing a Job in Up: Timing a Job Previous: Timing a Job
Zdzislaw Meglicki
2001-02-26