time - macOS
Overview
The time command executes a specified command or script and reports the time it takes to complete. It provides detailed performance metrics that can be valuable for optimizing code and identifying performance bottlenecks.
Syntax
time [-p] [-v] [-o file] [-a] [-l] [-f format] command [args...]
Options/Flags
- -p: Print both real and CPU time in the format “real user sys “.
- -v: Verbose mode. Print command output along with timing information.
- -o file: Write timing information to the specified file instead of stdout.
- -a: Append timing information to the specified file instead of overwriting it.
- -l: Print timing information in a long format, displaying additional details.
- -f format: Specify a custom output format string. See
man time
for details.
Examples
- Execute a command and print the real and CPU time:
time ls -l /usr/bin
- Execute a script and print the timing information to a file:
time ./my_script.sh -o timing.log
- Use verbose mode to display command output and timing information:
time -v python my_program.py
Common Issues
- Command not found: Ensure that the specified command is installed and available in the system path.
- Timing information not printed: Check if the correct options are specified. By default, only the real time is printed. Use
-p
for both real and CPU time. - Incorrect output format: Verify the specified custom format string using
man time
. The default format is “real %e user %U sys %S”.
Integration
- Combine with other commands to measure the performance of specific tasks:
time cp -R /src/ /dst/
- Create scripts to automate performance testing and generate reports:
#!/bin/sh
for i in {1..10}; do
echo "Iteration $i"
time ./my_program.py
done
Related Commands
- top: Monitor system performance and resource usage.
- perf: Perform low-level performance profiling and optimization.
- resourcelimits: Set resource limits for processes, including CPU time.