Using the time command to measure microprocessor use

Use the time command to understand the performance characteristics of a single program and its synchronous children.

The time command reports the real time, that is the elapsed time from beginning to end of the program. It also reports the amount of microprocessor time used by the program. The microprocessor time is divided into user and sys. The user value is the time used by the program itself and any library subroutines it calls. The sys value is the time used by system calls invoked by the program (directly or indirectly).

The sum of user + sys is the total direct microprocessor cost of executing the program. This does not include the microprocessor costs of parts of the kernel that can be said to run on behalf of the program, but which do not actually run on its thread. For example, the cost of stealing page frames to replace the page frames taken from the free list when the program started is not reported as part of the program's microprocessor consumption.

On a uniprocessor, the difference between the real time and the total microprocessor time, that is:
real - (user + sys)
is the sum of all of the factors that can delay the program, plus the program's own unattributed costs. On an SMP, an approximation would be as follows:
real * number_of_processors - (user + sys)

In approximately the order of diminishing size, the factors can be:

  • I/O required to bring in the program's text and data
  • I/O required to acquire real memory for the program's use
  • microprocessor time consumed by other programs
  • microprocessor time consumed by the operating system
In the following example, the program used in the preceding section has been compiled with -O3 to make it run more quickly. There is very little difference between the real (wall-clock) time required to run the program and the sum of its user and system microprocessor times. The program is getting all the time it wants, probably at the expense of other programs in the system.
# time looper
real    0m3.58s
user    0m3.16s
sys     0m0.04s
In the next example, we run the program at a less favorable priority by adding 10 to its nice value. It takes almost twice as long to run, but other programs are also getting a chance to do their work:
# time nice -n 10 looper
real    0m6.54s
user    0m3.17s
sys     0m0.03s
Note that we placed the nice command within the time command, rather than the reverse. If we had entered
# nice -n 10 time looper

we would have gotten a different time command (/usr/bin/time) with a lower-precision report, rather than the version of the time command we have been using, which is built into the ksh shell. If the time command comes first, you get the built-in version, unless you specify the fully qualified name of /usr/bin/time. If the time command is invoked from another command, you get /usr/bin/time.