Information Technology Reference
In-Depth Information
Measuring the CPU Time
The forthcoming subsections contain implementations of the trapezoidal algorithm
in many other languages. The relative computational efficiency of these implemen-
tations is of significant interest. The CPU time consumed by an algorithm is a good
measure of computational efficiency. Unix systems have a command
time
that can
be used to infer the timings of a program. In the present case one can write
unix> time ./int
and the output will be something like
real 0m4.367s
user 0m4.310s
sys 0m0.050s
“Time” on a computer is not a unique term. The time it takes for the
int
pro-
gram to execute, measured on a wall clock or wrist watch, is called
wall clock time
.
Sometimes this time measurement is called
elapsed time
or
real time
. This mea-
sure of time may not reflect the efficiency of the program well if there are many
other processes and users running concurrently on the machine. Therefore, other
time measurements are introduced. The
user time
is the time it takes to execute the
statements in the program, minus the time that is spent in the operating system on,
e.g., reading and writing files. The time consumed by the latter operations is called
system time
. The sum of the user and system time constitutes the
CPU time
, i.e.,
the time it takes to execute all statements in the program.
7
In the example above, the
CPU time is 4.310
C
0.05
D
4.360 s. The accuracy of such timings varies with the
computer system. To be on the safe side, the program should run for several seconds
(say, at least 5 s).
The current Fortran 77 program with 10,000 repetitive calls to the function
trapezoidal
, with
n D 1;000
, requires about 4 s on an IBM X30 laptop running
Linux and using the
g77
compiler. (Note that a “smart” compiler may be so smart
that it throws away repetitive calls unless we insert statements that require all the
calls!) For comparison purposes it is advantageous to use relative time measures.
We will hence report the timings of the other implementations as the actual CPU
time divided by the CPU time required by the Fortran 77 code.
Remark
The CPU time spent by a set of statements can in most languages also be measured
inside the program by a suitable function call. Unfortunately, Fortran has no unified
standard for doing this, although many Fortran compilers offer a timing function
(
g77
offers
dtime
). We therefore rely on the Unix command
time
for timing the
complete execution of a program.
7
This is almost correct: Child processes, typically launching a new program inside another
program, are not included in the CPU time, but this is of no relevance in the present simple program.