Timing on your computer

In order to determine the relative execution efficiency of various algorithms, you will need to use a timer package.  I have available (see below) for your use such a package written in C.  However, with advance approval from me, you may use other packages. Note that CPU time is used by your program (user time) as well as the system (system time). We will be interested in capturing the values of the user time usage only.

A problem:  On some systems, the timing function gives times in multiples of .01 seconds.  On others, the timing coarseness may be significantly smaller or larger.  (You should determine the timing coarseness for your machine.)  Thus, for example, on a system with .01 second timing coarseness, timing anything which takes less than one fiftieth of a second could result in more than a 50% error.  Your programs, for small values of n, should be easily this fast.

A solution:  When the algorithm to be timed is too fast to obtain an accurate measurement, the algorithm can be run many times in succession, with the cumulative time being measured.  This time can then be divided by the number of runs to obtain a more accurate measurement of the time required by an individual run.  As little as possible, other then the successive calls, should occur while the timing is taking place.  In fact, for particularly speedy code, the time spent updating the loop variable may be of the same order of magnitude as the time you are attempting to measure.  To account for this, you can subsequently time the loop with a (nearly) empty body and subtract this measure from your first measure, the net result reflecting more accurately the time spent within the code of interest. One should note that some optimizing compilers will optimize loops that contain an empty body by discarding them.

Timer package

Program file timer.c should be compiled and linked with your program system.  It defines the following routines:

Header file timer.h declares the routines that are defined in timer.c and it should be included in all program files that invoke any of those routines.

Timing coarseness and computer speed

My timex program (which uses the timer package described above) measures the time of one clock tick and also how long it takes to count to 1 billion.  The program can be compiled optimized, or not optimized, by the following:
gcc -O9 timex2.c timer.o -o timex2 
gcc     timex2.c timer.o -o timexu2

You should write your own version of timex, but the executable of my version is available for downloading:

machine count 1 billion
optimized not optimized
PC TIMEX2.EXE TIMEXU2.EXE
Solaris 8 timex2.solaris timexu2.solaris
Solaris 10 timex2.s10 timexu2.s10

The results of running this program are summarized in the following table.  Running such a program several times on the same machine might sometimes give slightly different results.  (Why is that?)  Send me your results for other machines.  Specify whether you used my or another timer package.

PC machine clock tick
(secs)
count to 1 billion (secs)
optimized not opt
Opti755 Intel Duo 2.4GHz, Win XP .05495 = 5/91  0.27 2.31
GX270 Intel 2.8GHz, Win XP .05495 = 5/91  0.49 1.43
GX280 Intel 2.8GHz, Win XP .05495 = 5/91  0.49 2.09
Intel 2.4GHz, Win XP .05495 = 5/91  0.60 1.65
Intel 2.8GHz, Solaris 10 .00001  0.57 2.25
Sun machine clock tick
(secs)
count to 1 billion (secs)
optimized not opt
Ultra AX-i2 (500Mhz), Solaris 8 .01000  2.05 22.39
Ultra 5 (400Mhz), Solaris 8 .01000  2.52 27.84


Dan Hirschberg
Computer Science Department
University of California, Irvine, CA 92697-3435
dan (at) ics.uci.edu
Last modified: Apr 1, 2009