$_ CLI In Depth
Learning progress 0%
lesson performance tmux vim git

Measuring with time

6 min read Module 18: Performance, Debugging & Power Tools Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:10

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.

BASH1 lines
time sleep 1

The 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.

BASH1 lines
{ time sleep 1; } 2> timing.txt

The 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.

BASH1 lines
TIMEFORMAT=%R; { time sleep 0.2; } 2> elapsed.txt

The 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.

BASH1 lines
{ time -p sleep 0; } 2> posix.txt

Timing 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.

Warning

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.

Real, user and sys: where the time went

Visualizer

time reports three clocks. Reading them tells you whether a command was waiting, computing, or making system calls.

1 / 3
The three numbers `time` prints
ClockCountsFor sleep 1
realwall clock time, start to finishabout 1 s
userCPU time spent in the program’s own codeabout 0 s
sysCPU time spent in the kernel for its system callsabout 0 s

time prints three numbers once the command ends. real is the wall clock. user and sys add up the CPU time the process actually used.

time sleep 1
Terminal lab · 3 steps · real Linux in your browser

Time commands with the time keyword

Use the time keyword, change its output format with TIMEFORMAT, and switch to the POSIX format with -p.

01
Time a command with the keyword
time is a shell keyword, not a separate program. It reports real, user, and system time on standard error. Group a short sleep and send the report to a file: ``bash { time sleep 1; } 2> timing.txt `` Open timing.txt. It has three lines that start with real, user, and sys.
Show hint for step 1
The braces group the command so that the report of time is redirected with the group. Redirection after time would also work.
02
Print only the elapsed seconds
TIMEFORMAT is a variable that sets the layout of the time report. %R is the real (elapsed) time in seconds. Set it and time a short sleep: ``bash TIMEFORMAT=%R { time sleep 0.2; } 2> elapsed.txt `` Open elapsed.txt. It should hold one number with a decimal point, such as 0.201.
Show hint for step 2
Set TIMEFORMAT before the timed command. Assigning it after has no effect on that command.
03
Use the POSIX output format
The -p option of time switches to the portable format defined by POSIX, with one value per line and no minutes: ``bash { time -p sleep 0; } 2> posix.txt `` Open posix.txt. The lines start with real, user, and sys followed by a space and a number.
Show hint for step 3
The POSIX format puts one space between the label and the value, so the labels are easy to parse in scripts.
Check yourself Question 1 of 4
Concept Check Which clock is kernel time recall

Which of the three time values is the CPU time the kernel spends on the program's behalf?

Recap & Key Takeaways

Real time is wall-clock time, user time is CPU time in the program, and sys time is CPU time the kernel spends on its behalf.
The time keyword is part of bash, and its report goes to standard error, so group the command with braces to redirect it.
TIMEFORMAT controls the layout, with %R for elapsed seconds, and time -p prints the fixed POSIX format.
Measure several runs and compare user and system time along with real time before deciding that a change helped.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue