time

performance monitoringLinux/Unix
The time command is one of the most frequently used commands in Linux/Unix-like operating systems. time Measure program execution time

Quick Reference

Command Name:

time

Category:

performance monitoring

Platform:

Linux/Unix

Basic Usage:

time [options] [arguments]

Common Use Cases

    Syntax

    time [options] command [arguments...]

    Options

    Option Description
    Note: The options below are for GNU time (/usr/bin/time). The shell built-in time command usually has limited or no options.
    -f FORMAT, --format=FORMAT Specify output format, with placeholders for various statistics
    -p, --portability Use the portable output format
    -o FILE, --output=FILE Write the resource use statistics to FILE instead of stderr
    -a, --append Append to the output file instead of overwriting it
    -v, --verbose Give very verbose output about all the program knows about
    --help Print a usage message and exit
    --version Print version information and exit
    Format Specifiers for -f option (GNU time)
    %E Elapsed real time (in [hours:]minutes:seconds)
    %e Elapsed real time (in seconds)
    %U Total number of CPU-seconds that the process spent in user mode
    %S Total number of CPU-seconds that the process spent in kernel mode
    %P Percentage of the CPU that this job got (user + system times / elapsed time)
    %M Maximum resident set size of the process during its lifetime (in Kbytes)
    %t Average resident set size of the process (in Kbytes)
    %K Average total (data+stack+text) memory use of the process (in Kbytes)
    %D Average size of the process's unshared data area (in Kbytes)
    %p Average size of the process's unshared stack (in Kbytes)
    %X Average size of the process's shared text (in Kbytes)
    %Z System's page size (in bytes)
    %F Number of major page faults that occurred
    %R Number of minor page faults that occurred
    %W Number of times the process was swapped out of main memory
    %c Number of times the process was context-switched involuntarily
    %w Number of waits: times that the program was context-switched voluntarily
    %I Number of file system inputs by the process
    %O Number of file system outputs by the process
    %r Number of socket messages received by the process
    %s Number of socket messages sent by the process
    %k Number of signals delivered to the process
    %C Name and command-line arguments of the command being timed
    %x Exit status of the command

    Examples

    How to Use These Examples

    The examples below show common ways to use the time command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    # Time a simple command time ls -la
    # Time a more complex command with pipes time find /etc -name "*.conf" | wc -l
    # Time a command and save the output to a file (command output only) time ls -la > output.txt
    # Advanced Examples Advanced
    # Use GNU time with verbose output (if GNU time is installed) /usr/bin/time -v ls -la
    # Format the output of GNU time /usr/bin/time -f "Time: %E, Memory: %M KB" ls -la # Time a script execution time ./script.sh # Time a resource-intensive command time sort -u large_file.txt # Append timing information to a log file /usr/bin/time -a -o timing.log ls -la # Time a command that might take hours time sleep 2 # Replace with your long-running command # Time a command multiple times for benchmarking for i in {1..5}; do time command_to_benchmark; done # Time a command with specific formatting for easier parsing /usr/bin/time -f "%e,%U,%S,%M" command > output.txt 2> timing.txt # Compare time taken by two different commands echo "Command 1:"; time command1 echo "Command 2:"; time command2 # Time a command that uses sudo time sudo apt-get update # Time a command and show memory usage statistics /usr/bin/time -v command

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `time` command in Unix and Linux is used to measure how long a program takes to execute. It provides timing statistics about the command execution, which is useful for performance analysis, benchmarking, and resource utilization monitoring. There are actually two different `time` commands that you might encounter: 1. **Shell built-in time**: Most shells (like Bash) include a simple built-in `time` command that provides basic timing information. This is what you typically get when you run `time` directly in your shell. It usually reports three values: - **real**: The actual elapsed wall-clock time from start to finish - **user**: The amount of CPU time spent in user mode - **sys**: The amount of CPU time spent in kernel mode 2. **GNU time** (`/usr/bin/time`): This is a separate program that provides more detailed information about resource usage. To explicitly use this version, specify the full path (`/usr/bin/time`). GNU time offers many additional metrics and formatting options. The distinction between "user" and "sys" time is important for understanding where a program spends its time: - **User time** represents CPU time spent executing the program's own code. - **System time** represents CPU time spent in kernel operations requested by the program (e.g., system calls for I/O operations). The sum of user and system time may be less than the real time for programs that: - Wait for I/O operations - Sleep or wait for other events - Run in parallel across multiple CPU cores Conversely, the sum may exceed real time for multi-threaded programs that utilize multiple CPU cores simultaneously. The GNU version of `time` provides extensive additional information, including memory usage, page faults, context switches, and I/O operations, making it valuable for detailed performance profiling. When using `time` for benchmarking, it's important to run the command multiple times to account for variations due to system load, caching effects, and other factors that can influence execution time. The `time` command is a simple yet powerful tool for developers and system administrators to identify performance bottlenecks, compare algorithm efficiency, and monitor resource usage without requiring complex profiling tools.

    Related Commands

    These commands are frequently used alongside time or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the time command works in different scenarios.

    $ time
    View All Commands