×


Time Command How to know execution time in Linux

Time command shows the time taken for the execution of a program or script.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform Software Installation on Linux Servers.

In this context, we shall look into different ways of execution time in Linux.


What does Time Command mean in Linux?

There are situations where we want to know how long it took for a command or script to execute. We can track the start and end times and calculate the difference to get the duration. This can easily accomplish using the time command.

Basically, when Time Command is used in Linux, you will see the following information about a process;

i. real time – the total execution time. This is the time elapsed between invocation and termination of the command.

ii. user CPU time – the CPU time used by your process.

iii. system CPU time – the CPU time used by the system on behalf of your process.


The command-line is full of a bunch of small, single-purpose utilities that can help eliminate the need to write any additional code ourselves.

The time command shows the time taken for the execution of a program or script.


Step 1 – Time Command Execution

To find the execution duration, we prefix our command with time. However, how we execute time depends on our operating system.

The time exists both as a built-in command for some shells, like bash and zsh, as well as a stand-alone command, known as GNU time which has different arguments than the time built into shells.

We use the following command to see how time works on our system:

$ type -a time

We will see output like the following:

Output
time is a shell keyword
time is /usr/bin/time

If we want to use the GNU version of time instead, we can prefix it with a backslash:

$ \time

If we don’t, our shell will use its built-in version instead.

Both methods serve the same purpose, although the GNU version has more advanced formatting options.

Let us explore timing program execution by using the tree command on the root / of our file system, which will list out a visual tree of all of our files and directories.


The tree command does not come as default on many systems. However, we can install it on Ubuntu and Debian systems with apt:

$ sudo apt install tree

Once done, use it to look at all the files on the system, but prefix it with time to see how long it takes:

$ time tree /

We will see the file information scroll through. Eventually, it will stop and present with the time taken:

Output
# The output of tree will scroll by here.
166590 directories, 1568127 files
tree / 12.24s user 10.37s system 66% cpu 33.827 total

Notice the command executed, tree /, is echoed back by the time command as well. The output shows a few pieces of info, but for now, we focus on the total and the number just prior:

Output
tree / 12.24s user 10.37s system 66% cpu 33.827 total

That is how long the command took to execute in seconds.

It also works if we happen to cancel a command with CTRL+C. If we were to run time tree / again and hit CTRL+C, it will stop the scroll. Subsequently, it will present the results for the time the command was executing before the stop.


[Queries regarding execution in Linux? We are available!]


Step 2 – Understanding Resource Usage

The output from time includes three values, in addition to the total duration.

Output
tree / 12.24s user 10.37s system 66% cpu 33.827 total

The first is the total amount of time (in CPU-seconds) that the command spent in user mode. That is the value with the user after it.

The next is the amount of time (in CPU-seconds) that the command spent in the system, or kernel mode.

Finally, the percentage of the CPU that was allocated to the command, suffixed with the CPU.

The difference between user and system time is that CPU usage breaks down by access levels.

When we execute the code in user mode, it does not have direct access to hardware or reference memory and must rely on APIs of the system for delegation. This is how most code runs on our system, and due to its isolation, crashes are always recoverable.

Kernel mode, on the other hand, is when code being executed has unrestricted access to the system hardware.


This mode is pretty much reserved for the most trusted functions of the operating system. Because of having complete access, when things crash in kernel mode, they crash bad and tend to take the system down with them.


[Still, having issues with time commands in Linux? We’d be happy to assist you.]


Conclusion

This article will guide you on how to use the time command in Linux which is important in determining the duration of execution of a command.