time
Command: Measuring Execution Time
Summary
The time
command measures the execution time of a given command or program. It provides information about the real time, user CPU time, and system CPU time used during the command's execution.
Introduction
The time
command is a built-in Linux utility used to measure the amount of time a command takes to execute. This can be invaluable for performance analysis, identifying bottlenecks, and comparing the efficiency of different approaches to a task. It reports three primary time values:
- Real time (wall clock time): The actual elapsed time from the start to the end of the command. This includes time spent waiting for I/O, other processes, or user input.
- User CPU time: The amount of time the CPU spent executing instructions in user mode on behalf of the command.
- System CPU time: The amount of time the CPU spent executing instructions in kernel mode on behalf of the command (e.g., system calls).
Use case and Examples
Basic Usage
This command executes thesleep 5
command (which pauses for 5 seconds) and then reports the time taken. You'll see the real, user, and system times reported after the 5 seconds. Timing a script
This runs the shell scriptmy_script.sh
and displays the execution time summary after completion. This is useful for measuring the performance of your scripts. Timing a complex command pipeline
This pipes the output offind
to wc -l
to count the number of .txt
files on the system, and then reports the total execution time of the entire pipeline. This demonstrates how to time more complex command sequences. Appending output to a file
This command redirects the output ofls -l /usr/bin
to timing_results.txt
and the timing information to standard error. This is useful for automating performance testing and logging. Commonly used flags
Flag | Description | Example |
---|---|---|
-p | Use the portable output format. | time -p ls -l |
-v | Verbose output, displaying more detailed statistics. | time -v ls -l |
--format=FMT | Specifies a custom output format using variables like %e, %U, %S, etc. | time --format="%e seconds real, %U seconds user" ls -l |