Time how long something takes
Three numbers, and two different commands called time
Problem: You want to know how long a command took, and the answer comes back as three numbers you did not ask for.
Solution:
time sleep 0.2
real 0m0.204s
user 0m0.001s
sys 0m0.002s
How it works:
The three numbers measure different things. Reading them means reading the gaps between them.
real is wall-clock time: what a stopwatch would have said. It includes time the command spent
waiting on a disk, a network or another process, and time the machine spent on something else
entirely.
user is CPU time spent running the command's own code.
sys is CPU time spent inside the kernel on the command's behalf, reading files and allocating
memory.
sleep is the clearest case: two tenths of a second of real and almost no CPU at all, because it
did nothing but wait. The reverse pattern, user close to real, is a command that was busy the
whole time. And user plus sys exceeding real is not a mistake: it means the work ran on
several cores at once.
Variations:
The time you type is not a program. It is part of the shell's grammar, so it can time a whole
pipeline:
type time
command -v time
time is a shell keyword
time
Debian ships a separate /usr/bin/time in the time package, which is not installed by default.
It reports the same measurements differently and knows things the keyword does not:
/usr/bin/time sleep 0.2
0.00user 0.00system 0:00.20elapsed 0%CPU (0avgtext+0avgdata 1288maxresident)k
0inputs+0outputs (0major+85minor)pagefaults 0swaps
maxresident is the peak memory the command used, in kilobytes. The keyword reports no memory at
all, so that figure is the reason to install the program. -f chooses what to print:
/usr/bin/time -f 'elapsed %es, max RSS %MkB' sleep 0.2
elapsed 0.20s, max RSS 1352kB
%e is elapsed seconds, %M peak resident memory and %P the CPU percentage. -v prints
everything it knows in a long block, including page faults and context switches.
Group the whole thing to capture it:
{ time sleep 0.2 ; } 2> timing.log
cat timing.log
real 0m0.201s
user 0m0.000s
sys 0m0.002s
The braces make one compound command whose standard error the redirect can reach. /usr/bin/time
needs none of this, since -o file is one of its options.
For anything that finishes quickly, a single run tells you very little: process startup, a cold
cache and whatever else the machine was doing swamp the measurement. Run it enough times to see the
spread before believing a difference, and compare user rather than real when the machine is
busy, since real is measuring the machine's load as much as your command.