LinuxCommandLibrary

time

Measure execution time of a command

TLDR

Run the command and print the time measurements to stdout

$ time [command]
copy

Create a very simple stopwatch (only works in Bash)
$ time read
copy

SYNOPSIS

time [options] command [arguments...]

PARAMETERS

-p, --portability
    Use the portable output format, providing a simpler and standardized report suitable for parsing across different systems.

-o FILE, --output=FILE
    Write the time statistics to FILE instead of standard error. Useful for logging or programmatic access to the results.

-a, --append
    When used with -o, append the output to the specified file rather than overwriting it, allowing multiple measurements to be logged.

-f FORMAT, --format=FORMAT
    Specify the output format using a string that contains special escape sequences (e.g., %U for user time, %S for system time). Gives full control over the displayed metrics.

-v, --verbose
    Provide a verbose output, displaying all available information about the command's resource usage in a human-readable format, including memory and I/O statistics.

-q, --quiet
    Do not report the exit status of the command itself, only the timing information. This is useful in scripts where the command's success/failure is handled separately.

-h, --help
    Display a help message with command usage and options, then exit.

--version
    Output version information for the GNU time utility and exit.

DESCRIPTION

The time command executes a specified command and reports a summary of the system resources used by that command. This includes the real (wall clock) time elapsed, the user CPU time spent, and the system CPU time spent. It's an invaluable tool for performance profiling, allowing developers and system administrators to understand how efficiently a program utilizes CPU resources and how much time it spends waiting for I/O or other system operations. While many shells have a built-in time command with basic functionality, the external GNU time utility (typically found at /usr/bin/time) offers more advanced features, such as customizable output formats and the ability to output results to a file, making it more flexible for scripting and detailed analysis.

CAVEATS

The behavior and available options of time can vary significantly between shell built-ins (e.g., Bash, Zsh) and the external GNU time utility (/usr/bin/time). Shell built-ins typically offer fewer features and a fixed output format. To ensure you're using the GNU time utility with its full capabilities, always invoke it explicitly as /usr/bin/time or use the command built-in (e.g., command time my_script.sh) to bypass the shell's built-in version.

BUILT-IN VS. EXTERNAL <I>TIME</I>

Many modern shells (Bash, Zsh, Dash) include a built-in time command. This built-in is executed directly by the shell, making it faster and sometimes reporting slightly different metrics than the external /usr/bin/time program. The built-in usually has limited options. To force the use of the external GNU time utility, you can specify its full path (/usr/bin/time) or use the command built-in (command time) to bypass the shell's built-in version.

UNDERSTANDING TIME METRICS

The output of time typically includes three key metrics:

  • Real Time: The actual wall-clock time elapsed from the start to the end of the command. This includes time spent waiting for I/O, other processes, or network activity.
  • User CPU Time: The amount of CPU time spent in user mode (executing the program's own code) by the process. This is the time the CPU spent actively running your code.
  • System CPU Time: The amount of CPU time spent in kernel mode (performing system calls like file I/O, process creation, or network operations) on behalf of the process.
Comparing these values helps identify bottlenecks; high real time with low user/sys time suggests I/O or network waits, while high user/sys time suggests CPU-bound operations.

HISTORY

The time command has been a fundamental utility in Unix-like operating systems since their early days, providing a simple way to measure program execution performance. Over time, different implementations emerged, most notably the shell built-in versions (which are typically simpler and faster as they don't require spawning a new process) and the external GNU time utility. The GNU version, developed as part of the GNU Project, significantly expanded its capabilities, introducing customizable output formats and a wider range of reported metrics, making it a more powerful tool for detailed analysis and scripting.

SEE ALSO

perf(1), strace(1), ltrace(1), top(1), htop(1), ps(1)

Copied to clipboard