Bobcares

For every $500 you spend, we will provide you with a $500 credit on your account*

BLACK FRIDAY SPECIAL EXTENSION

*The maximum is $4000 in credits, Offer valid till December 6th, 2024, New Customers Only, Credit will be applied after purchase and expires after six (6) months

For every $500 you spend, we will provide you with a $500 credit on your account*

BLACK FRIDAY SPECIAL EXTENSION

*The maximum is $4000 in credits, Offer valid till December 6th, 2024, New Customers Only, Credit will be applied after purchase and expires after six (6) months

Time Command – How to check execution time in Linux

by | Dec 23, 2020

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

As part of our Server Management Services, we assist our customers with several Linux queries.

Today, we will discuss the various aspects of execution time in Linux.

 

Time Command

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.

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. Furthermore, let us see how our Support Engineers go about with this query.

Time Command

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? Contact us now!]

 

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, stuck with time commands? We’d be happy to assist]

 

Conclusion

To conclude, we discussed how long commands and scripts take to run, and where that time goes. Today, we saw an effective method our Support Techs employ to use the time command.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.