Understanding Time Command in Linux


As a Linux user, you must have come across time command. It is a simple yet powerful command that allows you to measure execution time of a process. Whether you are a developer, system administrator, or just a curious user, understanding how time command works is essential for optimizing your workflow and identifying bottlenecks in your system. In this article, we will dive deep into time command in Linux and explore its various use cases.

What is time command?

The time command is a Linux utility that measures time it takes for a given command to execute. command accepts a single argument, which is command you want to measure. output of time command includes following information −

  • Real time − actual elapsed time, including time spent waiting for I/O and other processes.

  • User time − amount of CPU time spent in user-mode code.

  • System time − amount of CPU time spent in kernel-mode code.

The time command is available on all major Linux distributions, including Debian, Ubuntu, CentOS, and Fedora.

Using time command

To use time command, simply type "time" followed by command you want to measure. For example, to measure execution time of "ls" command, you would run −

time ls

The output will look something like this −

real    0m0.003s
user    0m0.000s
sys     0m0.003s

Here, real time is 0.003 seconds, user time is 0.000 seconds, and system time is 0.003 seconds. real time is most important metric since it includes all time spent waiting for I/O and other processes. user and system times are also useful for identifying performance bottlenecks, but they are less important than real time.

The time command also works with complex commands that include pipes, redirection, and other shell features. For example, you can measure execution time of a pipeline that includes "grep" and "wc" commands like this −

time cat /var/log/syslog | grep "error" | wc -l

The output will look something like this −

1584
real    0m0.013s
user    0m0.010s
sys     0m0.007s

Here, pipeline returns number of lines in syslog file that contain word "error", and time command measures execution time of entire pipeline. Note that output of pipeline itself is not included in output of time command.

Options for time command

The time command also supports several options that allow you to customize its behavior. Here are some of most useful options −

  • -f format − This option allows you to specify a custom output format for time command. format string should include one or more conversion specifiers, such as %E for elapsed time, %U for user time, and %S for system time. For example, to display real time and CPU time in seconds, you can run −

time -f "%E real, %U user, %S sys" ls

The output will look like this −

0:00.00 real, 0.00 user, 0.00 sys
  • -o file − This option allows you to redirect output of time command to a file instead of standard output. For example, to save output of time command to a file called "output.txt", you can run −

time -o output.txt ls
  • -p − This option is used to format output for use in scripts or other programs. It prints real, user, and system times in seconds, separated by a space.

time -p ls

The output will look something like this −

real 0.003
user 0.000
sys 0.003
  • -v − This option allows you to save output of time command to a shell variable instead of printing it to standard output. For example, to save real time of "ls" command to a variable called "elapsed_time", you can run −

elapsed_time=$(time -v ls 2>&1 >/dev/null | grep "Elapsed (wall clock) time")

The output will look something like this −

Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.00
  • -a − This option allows you to display additional information about process being timed. This includes exit status of process, maximum resident set size (RSS), and number of voluntary and involuntary context switches. For example, to display this additional information for "ls" command, you can run −

time -a ls

The output will look something like this −

real    0m0.003s
user    0m0.000s
sys     0m0.003s
exit    0
voluntary_ctxt_switches  0
involuntary_ctxt_switches    1

Additional Use Cases for time Command

Apart from measuring execution time of a single command, time command can also be used to measure performance of a system or a script.

Measuring System Performance

You can use time command to measure overall performance of a system by running a command that stresses system's resources. For example, you can use dd command to read or write large amounts of data from or to a disk.

time dd if=/dev/zero of=/dev/null bs=1M count=1000

This command reads 1000 blocks of 1MB each from null device and writes them to null device. output of command includes real time, user time, and system time, as well as transfer rate and time taken to transfer data.

Measuring Script Performance

You can use time command to measure performance of a script by running it with time command. For example, suppose you have a Python script called "myscript.py" that performs a complex computation. You can measure execution time of script like this −

time python myscript.py

The output of command includes real time, user time, and system time taken to execute script.

Measuring Performance of a Loop

You can use time command to measure performance of a loop in a script by enclosing loop in curly braces and prefixing it with time command. For example, suppose you have a bash script that contains a loop that performs a complex computation. You can measure execution time of loop like this −

time {
for i in {1..10000}; do
   # complex computation
done
}

The output of command includes real time, user time, and system time taken to execute loop.

Conclusion

The time command is a versatile and useful tool for measuring execution time of commands in Linux. Whether you are debugging a slow script, optimizing a database query, or just curious about how long a command takes to run, time command can provide valuable insights into your system's performance. By mastering time command and its various options, you can become a more efficient and effective Linux user or system administrator.

Updated on: 24-Mar-2023

304 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements