Bobcares

WeSupport

Call Us! 1-800-383-5193
Call Us! 1-800-383-5193
Call Us! 1-800-383-5193

Need Help?

Emergency Response Time custom

Our experts have had an average response time of 11.06 minutes in March 2021 to fix urgent issues.

We will keep your servers stable, secure and fast at all times for one fixed price.

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 *

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF