Use the time command to understand the performance characteristics of a single program and its synchronous children. It reports the real time, that is the elapsed time from beginning to end of the program. It also reports the amount of CPU time used by the program. The CPU 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 CPU cost of executing the program. This does not include the CPU 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 CPU consumption.
On a uniprocessor, the difference between the real time and the total CPU 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:
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 CPU 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.
Take several considerations into account when you use either the time or the timex command:
On an SMP the real, or elapsed time may be smaller than the user time of a process. The user time is now the sum of all the times spent by the threads or the process on all processors.
If a process has four threads, running it on a uniprocessor (UP) system shows that the real time is greater than the user time:
# time 4threadedprog real 0m11.70s user 0m11.09s sys 0m0.08s
Running it on a 4-way SMP system could show that the real time is only about 1/4 of the user time. The following output shows that the multithreaded process distributed its workload on several processors and improved its real execution time. The throughput of the system was therefore increased.
# time 4threadedprog real 0m3.40s user 0m9.81s sys 0m0.09s