Linux Command Reference ยท Processes and Signals
Bash time command
Reference for the Bash time keyword and GNU time: measure real, user and system time of a command or pipeline, customize TIMEFORMAT, and read peak memory with /usr/bin/time.
time runs a command and reports how long it took. There are two versions. The one you normally use is a Bash keyword: it times a whole pipeline and prints three lines, real, user and sys, to standard error. The other is the program /usr/bin/time (GNU time), which has options and can also report memory use and exit status.
The three numbers answer different questions. real is the wall-clock time you waited. user is CPU time spent running the program's own code, and sys is CPU time spent in the kernel on its behalf. On several cores user + sys can exceed real; while waiting on a disk, network or a sleep, it is much smaller.
time PIPELINE /usr/bin/time [-v] [-f FORMAT] [-o FILE] COMMAND [ARG]...
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
GNU time is not installed on the live runner, so this one is read-only:
/usr/bin/time -f '%e s, %M KB peak, exit %x' sort big.txt > /dev/null # 0.42 s, 15840 KB peak, exit 0
Syntax
time [-p] PIPELINE
TIMEFORMAT=FORMAT
| Piece | Description |
|---|---|
time -p |
Portable POSIX format: real 0.50, user 0.00, sys 0.00, one per line, seconds. |
TIMEFORMAT |
Variable that sets the Bash output format. |
%R, %U, %S |
Real, user and system seconds. |
%P |
CPU percentage, (user+sys)/real. |
%lR, %lU, %lS |
Long form: 1m2.500s. |
%[N]R |
Number of decimals from 0 to 3. |
%% |
A literal percent sign. |
GNU /usr/bin/time has its own placeholders: -f with %e (elapsed seconds), %U, %S, %M (peak memory in KB), %x (exit status), and -v for a full report.
Returns: the exit status of the timed command or pipeline.
Usage notes
- Output goes to stderr. To capture it, redirect the group:
{ time cmd; } 2> timing.txt. Putting2>aftertime cmdonly redirects the command's own errors. - Keyword vs program.
timealone is the keyword;command timeor\timeor/usr/bin/timeruns the program. Only the program offers-vand memory data. - Pipelines. The Bash keyword measures the whole pipeline. The external program times one simple command, not shell syntax like pipes.
- Not a benchmark by itself. One run is noisy: file caches, CPU frequency and other processes matter. Repeat several times and compare the minimum or median.
- Resolution. Times are accurate to a few milliseconds. For shorter things, loop the command many times.
- Subshell trap.
timeapplies to the command that follows, sotime cd /tmptimes a builtin;time (a; b)times the group. - dash (
/bin/sh) has notimekeyword, so portable scripts need/usr/bin/time.