CS 4953 Experimentation in Computer Science Notes: Times and Timers


Copies of these programs can be found here

1. Times in UNIX:

Time in seconds since the Epoch:
Epoch: midnight, January 1, 1970 (Coordinated Universal Time or UTC or GMT)
The C standard defines the following functions:
Note: These return values in static objects and so they are not thread safe.

Look at programs simpletiming, timeprint, and badtiming.

The struct tm:

   int tm_sec;       /* seconds after the minute [0,60] */
   int tm_min;       /* minutes after the hour [0,59] */
   int tm_hour;      /* hours since midnight [0,23] */
   int tm_mday;      /* day of the month [1,31] */
   int tm_mon;       /* months since January [0,11] */
   int tm_year;      /* years since 1900 */
   int tm_wday;      /* days since Sunday [0,6] */
   int tm_yday;      /* days since January 1 [0,365] */
   int tm_isdst;     /* flag indicating Daylight Savings Time */

Thread safe versions:

  char *asctime_r(const struct tm *restrict timeptr, char *restrict buf);
  char *ctime_r(const time_t *clock, char *buf);
  struct tm *gmtime_r(const time_t *restrict timer,
                       struct tm *restrict result);
  struct tm *localtime_r(const time_t *restrict timer,
                       struct tm *restrict result);
Times in POSIX:XSI --- struct timeval

   time_t   tv_sec;   /* seconds since the Epoch */
   time_t   tv_usec;  /* and microseconds */

int gettimeofday(struct timeval *restrict tp, void *restrict tzp);
The second parameter must be NULL.
Returns the time since the Epoch.

Look at gettimeofdaytiming and gettimeofdaytest.

Here is a typical result of gettimeofdaytest on an Ultra 10, 440 Mhz:

Found 20 differences in gettimeofday:
53 calls to gettimeofday were required
 0:          1 microseconds
 1:          5 microseconds
 2:          1 microseconds
 3:          1 microseconds
 4:          1 microseconds
 5:          1 microseconds
 6:          1 microseconds
 7:          1 microseconds
 8:          1 microseconds
 9:          1 microseconds
10:          1 microseconds
11:          1 microseconds
12:          1 microseconds
13:          1 microseconds
14:          1 microseconds
15:          1 microseconds
16:          1 microseconds
17:          1 microseconds
18:          1 microseconds
19:          1 microseconds
The average difference is 1.200000

Times in POSIX:TMR --- struct timespec

    time_t  tv_sec;  /* seconds */
    long    tv_nsec; /* nanoseconds */
POSIX:TMR uses "clocks" represented by variables of type clockid_t.
Most common one is CLOCK_REALTIME

   int clock_getres(clockid_t clock_id, struct timespec *res);
   int clock_gettime(clockid_t clock_id, struct timespec *tp);
   int clock_settime(clockid_t clock_id, const struct timespec *tp);
clock_getres gives the resolution for setting the clock.
This is not necessarily time resolution for getting the time with clock_gettime.

Look at clockrealtimetiming and clockrealtimetest.

Here is typical output of clockrealtimetest on a SPARC Ultra 10:

Clock resolution: 10000000 nanoseconds
Found 20 differences in CLOCK_REALTIME:
21 calls to CLOCK_REALTIME were required
 0:        791 nanoseconds
 1:       6152 nanoseconds
 2:        485 nanoseconds
 3:        456 nanoseconds
 4:        450 nanoseconds
 5:        473 nanoseconds
 6:        473 nanoseconds
 7:        482 nanoseconds
 8:        509 nanoseconds
 9:        482 nanoseconds
10:        481 nanoseconds
11:        482 nanoseconds
12:        509 nanoseconds
13:        482 nanoseconds
14:        482 nanoseconds
15:        482 nanoseconds
16:        509 nanoseconds
17:        482 nanoseconds
18:        482 nanoseconds
19:        482 nanoseconds
The average difference is 781.300000
Note the typical resolution of about 1/2 microsecond and no two calls gave the same answer.

The time function

clock_t times(struct tms *buffer);
Returns the elapsed time in clock ticks since some arbitrary time.
The struct tms contains:
  clock_t   tms_utime;  /* user CPU time of process */
  clock_t   tms_stime;  /* system CPU time on behalf of process  */
  clock_t   tms_cutime  /* user CPU time of process and terminated children */
  clock_t   tms_cstime; /* system CPU time of process and terminated children */

Look at cpufraction and timechild.

Typical number of clock ticks per seconds is only 100, so this is not too useful.

timechild cpufraction
The number of ticks per second is 100.000000
Total CPU time for operation is 3.130000 seconds
Fraction of CPU time used is 0.695556
cpufraction used 315 clock ticks or 3.150000 seconds

High resolution time in Solaris: gethrtime

     #include <sys/time.h>
     hrtime_t gethrtime(void);
The gethrtime() function returns the current high-resolution real time.
Time is expressed as nanoseconds since some arbitrary time in the past;
It is not correlated in any way to the time of day. The hires timer is ideally suited to performance measurement tasks, where cheap, accurate interval timing is required.