Skip to content

time Command Cheat Sheet

time runs a command and then summarizes the system resource usage (mainly execution time).


Synopsis

time [options] command [arguments...]

Basic Output

time sleep 2
# Output (Shell builtin):
# real    0m2.003s
# user    0m0.000s
# sys     0m0.000s

Metrics Explained

  1. Real (Wall-clock): Actual time elapsed from start to finish. Includes waiting for I/O, network, or other processes.
  2. User: CPU time spent in user-mode code (your program's logic).
  3. Sys: CPU time spent in kernel-mode (system calls).

If User + Sys << Real, the process is I/O bourne (waiting on disk/network).


Shell Builtin vs Binary

  • Bash time: Simple, minimal options.
  • GNU /usr/bin/time: Highly configurable, detailed stats (memory, page faults).

To use the binary:

/usr/bin/time -v command
Verbose Output includes: - Maximum resident set size (RAM usage) - File system inputs/outputs - Page faults - Swaps


Custom Formatting (-f)

Only available in /usr/bin/time.

/usr/bin/time -f "Time: %E, Mem: %M KB" ls

Output to File (-o)

Save the stats to a file instead of Stderr.

/usr/bin/time -o benchmark.txt make

Notes

  • Benchmarking: Run tests multiple times (e.g., inside a loop) for accuracy.
  • Precision: time overhead is minimal but non-zero.