In Linux, we can use the time command to measure the program’s execution time.
In this tutorial, we’ll take a look at how to use time and how to read its output.
We tested our examples in Bash and should also work in other POSIX-compatible shells unless otherwise noted.
2. Measuring Execution Time
We can measure execution time by prefixing any command with time:
This executes the program named “command” and outputs its run time:
real 0m2.998s user 0m0.063s sys 0m1.616s
The output consists of three components. Let’s go into these in more detail.
3.1. User Time
In Linux, commands can are either executed in user or system mode. Another name for system mode is kernel mode.
Some commands require direct and unrestricted access to the underlying hardware. These are executed in kernel mode. All other commands are executed in user mode.
user time represents the time that a command has executed in user mode. In our case, that was precisely 0.063 seconds.
3.2. System Time
Our command spent 1.616 seconds running in the system time mode. sys represents the time the executed command has spent running in system or kernel mode.
Real-time represents the actual time that elapsed while the command ran. This is the time that passed between pressing the enter key and termination of the program.
In the example above, it took our command 0 minutes and 2.998 seconds to execute. This is actually bigger than the sum of times spent in user and system mode. That’s because we’re running our command in a concurrent environment where Linux has to balance resources between multiple processes.
For multi-threaded processes that run on multiple CPU cores, real time measured might be smaller than the total time spent in user and system mode as reported by the individual threads.
By default, time formats its output to show hours, minutes and seconds, for example:
Because most programs’ execution time is below 1 hour, time will omit the hour part for execution times under 60 minutes.
With the GNU 1.7 version of time, we can format the output by use of the -f parameter that takes a formatting string. With a custom format we can print a lot of other information as well, for example:
time command -f '%E %W %r'
This will not only print the real execution time (%E) but also the number of times the process was swapped out of main memory (%W) and the number of socket messages the process received (%r).
A full list of all formatting options is available on the man page.
The GNU 1.7 version of time however is not POSIX compatible. Most shells, like Bash, ship with a POSIX-compatible version that doesn’t have this formatting option.
In this article, we’ve learned how to interpret the output of time. Each program we run spends time running in user or system mode or both.
Real represents the actually elapsed time from start to finish of our program – it might be smaller than the added totals of time spent in user and system mode.
The GNU 1.7 version of time comes with a lot of formatting options. However, most shells don’t ship with this version by default as it isn’t POSIX-compatible.