Three clocks
A command takes time in three different senses. The real time is the wall clock: the time that passed between starting and finishing, as you would measure with a stopwatch. The user time is the CPU time that the program spent running its own code. The system time is the CPU time the kernel spent on the program's behalf, such as reading files, allocating memory, and creating processes. The real time can be larger than the sum of the other two when a program waits for the disk, the network, or another process.
This difference matters. A program with high user time is busy computing, and a faster algorithm would help. A program with high system time is making many system calls, and a different read size or fewer file operations would help. A program with real time much larger than user plus system is mostly waiting, and the CPU is not the bottleneck at all.
The time keyword
time is a reserved word in bash, not an external program, so it is available in every bash session without installing anything. It takes a command or a whole pipeline and prints the three times when the command ends. The report goes to standard error, which is why it appears on the terminal even when the command output is redirected.
time sleep 1The output has a blank line, then the three lines real, user, and sys, each with minutes and fractional seconds. A sleep does almost no work, so its user and system times are tiny while its real time is about one second. That contrast is the simplest way to see what the three numbers mean.
Capturing the report
Because the report goes to standard error, you can capture it. Group the command with braces so that the redirection applies to the whole group, including the report. Without the braces, the redirection applies only to the command, and the report is printed to the terminal anyway.
{ time sleep 1; } 2> timing.txtThe file now holds the three lines, and the command output, if any, still goes wherever it was going before. That is useful when you time a script in a loop and collect all the reports in one file.
Controlling the format
The shell variable TIMEFORMAT sets the layout of the report. It uses percent escapes: %R is the real time in seconds, %U is the user time, %S is the system time, and %P is the share of a CPU used. Setting TIMEFORMAT to %R prints just the elapsed seconds, which is convenient for a script that records one number per run. Assign it before the timed command, because the shell reads it when the command finishes.
TIMEFORMAT=%R; { time sleep 0.2; } 2> elapsed.txtThe POSIX format is a fixed layout that scripts can parse on any system. The option -p selects it, and each line holds a label and a number with a decimal point and no unit.
{ time -p sleep 0; } 2> posix.txtTiming a pipeline
A pipeline is one unit for time. The report covers the whole pipeline, from the start of the first stage to the end of the last one. To find the slow stage, time the stages separately, or put each one into its own timed group. The total real time tells you how long the user waits, and the per-stage user times tell you where the CPU goes.
Repeat a measurement before you trust it. A single run can include cache effects, a background job, or a pause for the disk. Three runs that agree within a few percent are a much better basis for a decision than one fast run.
Measurement habits
Measure the same input each time, and change one thing at a time. Keep the timing output with the command that produced it, because a number without its command is hard to reproduce later. When you compare two versions of a tool, compare their user and system times as well as the real time, because a change that moves work from user to system time may not be a real improvement.
The lab machine has no man pages, so use the help builtin for the keyword: the command help time shows the options that bash supports. For the POSIX rules, the shell command language page on the Open Group site describes the -p format. The next lessons use time to frame the other tools in this module, starting with the measures of memory and CPU pressure.
When user time is larger than real time
On a machine with several cores, a program with several threads can accumulate more user time than real time, because the CPU clocks add up the seconds spent by every thread. A user plus system total greater than the real time is not an error. It means the work ran in parallel, and the ratio of the two numbers is a rough count of how many cores were busy. The percent sign in TIMEFORMAT, written as %P, reports that ratio directly, so a single-threaded command shows a value near one hundred percent and a parallel one shows more.
Timing from inside a script
Inside a script, the SECONDS variable counts whole seconds since the shell started, which is too coarse for short commands. Bash 5 also provides EPOCHREALTIME, which holds the current time in seconds with a microsecond fraction. Save its value before and after a command, then subtract the two numbers with awk or with bash arithmetic on the integer parts. The result is wall-clock time, the same thing the real line reports, and it works even when you need the number in the middle of a larger script instead of in a report file.
The overhead of the time keyword is negligible for any command that runs longer than a few milliseconds, so there is no need to avoid it for real measurements. The overhead matters only for very short commands, where the result is dominated by process creation, and a loop of many runs is a better measurement than one run.