Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

time Command Guide

time measures how long a command takes to run, reporting real (wall clock), user (CPU), and sys (kernel) time. It’s the first tool you reach for when you want to know whether that refactor actually made anything faster.

What is time, and Why Use It?

You changed a script, rebuilt a project, or are considering a bigger machine — but before spending money or effort, you want numbers. time gives you three numbers:

  • real — the wall-clock time from start to finish (what you feel)
  • user — CPU time spent inside the program’s own code
  • sys — CPU time spent in the kernel on the program’s behalf

If real is much larger than user + sys, your command spent most of its time waiting — on disk, on the network, or on a lock — not computing. That tells you where to optimize.

There are two times: the shell keyword (built into bash/zsh) and the /usr/bin/time binary. Both work; the keyword is more portable across shells.

The Shell Keyword

The simplest form — just put time in front of any command:

# How long does this take?
time sleep 2

# Time a build
time make

# Time a download
time curl -O https://example.com/bigfile.tar.gz

# Time a pipeline
time cat bigfile.txt | sort | uniq -c

Output looks like this:

real    0m2.003s
user    0m0.001s
sys     0m0.002s

Note: as a shell keyword it works on pipelines and compound commands without quoting. That’s a big advantage over the binary version.

Reading the Three Numbers

# A CPU-bound task: real ≈ user + sys
time yes > /dev/null

# An I/O-bound task: real >> user + sys (waiting on disk)
time dd if=/dev/zero of=/tmp/test.bin bs=1M count=1024
  • real is always the total elapsed time.
  • user + sys ≈ total CPU time across all cores. On a multi-core machine, user + sys can exceed real when a program uses several cores in parallel.
  • If real is close to user + sys on one core, the command is mostly serial.

Timing with the Binary Version

The /usr/bin/time binary (install with sudo apt install time on Debian or sudo dnf install time on Fedora) has extras the keyword lacks — memory usage, for example:

# GNU time: show max memory used
/usr/bin/time -v tar -czf backup.tar.gz /home

# Run and capture output separately
/usr/bin/time -o timing.txt -a make

The -v (verbose) flag prints a full report including maximum resident set size, page faults, and exit status — great for profiling.

Combining with Other Tools

time pairs naturally with the rest of the wiki:

# Is this command CPU-bound? Compare with watch
time ./my-script.sh
watch -n 1 'ps -o pid,pcpu,comm -p $(pgrep my-script)'

# Time each stage of a pipeline separately
time (tar -czf - /dir) | wc -c

# Time a command and log the result
{ time -p ./long-job.sh ; } 2> timing.log

Useful flags on the keyword version (bash):

# POSIX-style output (one line, machine-readable)
time -p make

# Format the output yourself
TIMEFORMAT='elapsed %3R seconds'
time make

TIMEFORMAT is a bash/zsh variable that controls the output format: %R is real seconds, %U user, %S sys.

Timing Scripts and Loops

# Time a loop that runs something 100 times
time for i in $(seq 1 100); do ./task.sh; done

# Time a function
time my_function arg1 arg2

# Time everything inside a subshell
time {
  ./step1.sh
  ./step2.sh
}

For repeated measurements, run it several times and look at the best and worst — the first run is often slower due to cold caches:

for i in 1 2 3; do time ./my-task.sh; done

Common Pitfalls

  • Timing a pipeline with the binary. /usr/bin/time cmd | grep x times only cmd, not the pipeline. Use the shell keyword, or quote: /usr/bin/time 'cmd | grep x' won’t work either — wrap in a subshell: /usr/bin/time bash -c 'cmd | grep x'.
  • One run isn’t enough. Filesystem caches make the second run much faster. Run repeatedly and judge by the median or best.
  • Expecting sub-millisecond precision. time on a trivial command measures mostly shell overhead. Use hyperfine or Python’s timeit for micro-benchmarks.
  • Forgetting background work. If a command spawns children and exits, time may stop counting when the parent exits. Check with ps.
  • Confusing real with CPU. A command can burn 8 CPU-seconds in 2 real seconds on 4 cores. That’s not a contradiction — it’s parallelism.
  • watch — poll a command while it runs
  • yes — a classic CPU-burner to test with
  • ps — inspect processes and their CPU time
  • top — live CPU usage per process
  • nohup — long jobs that outlive your session
  • scripting — putting timed commands in scripts

Cheatsheet

CommandWhat it does
time sleep 2Time any command
time makeTime a build
time -p cmdPOSIX one-line output
TIMEFORMAT='%3R s'; time cmdCustom output format
/usr/bin/time -v cmdVerbose report incl. max memory
/usr/bin/time -o log.txt -a cmdAppend timing to a file
time cmd1 | cmd2Time a whole pipeline (keyword)
time { a; b; }Time a group of commands