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
- Real (Wall-clock): Actual time elapsed from start to finish. Includes waiting for I/O, network, or other processes.
- User: CPU time spent in user-mode code (your program's logic).
- 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
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:
timeoverhead is minimal but non-zero.