17.10. Timing

Often users want to collect timing information about their programs. For example, a user may want to measure whether a change to a program improves its performance. The time command is one way to collect timing information about a process. It takes a command line argument that is the executable command line of the program or command to time, and it outputs timing information of the execution of the program or command. Here is an example of timing a run of the program executable gol that takes two command line arguments (oscillator_500_500.txt and 0):

$ time ./gol oscillator_500_500.txt 0
real	0m3.814s
user	0m3.725s
sys	0m0.016s

The timing output is given as three different time values: real is the total execution time, sometimes referred to as wall time because it corresponds to the time from start to completion as if one was measuring runtime with a clock; user is the portion of total time that the process spent executing in user-mode; and sys is the portion of time that the process spent executing is system-mode (see Chapter 13: The Operating System for more information about user and system mode). Note that the user time plus the sys time do not add up to real time. This is because during part of a processes total execution time it is blocked waiting for certain events like I/O or waiting for its turn to be scheduled by the OS to run on a CPU core. When a process is blocked, it does not accumulate user or sys time, but it does accumulate real time.

In this example, the timed gol process spends most of its time running on a CPU core (its user + sys time is almost all of its real time). For applications that do a lot of I/O, their user + sys time may be much less than their real time.

Often, the -p option is used with the time command. It displays the timing output in a format that is compliant with a POSIX standard, so that every system displays the timing results in the same format when time is run with -p. This is particularly helpful for writing programs that do post-processing time output (e.g., computing the average over a set of timed program runs) that will work identically across systems.

17.10.1. References

For more information see: