Linux Tips

How To Discover The Execution Time Of A Command Or Course of In Linux

You in all probability know the beginning time of a command/course of and how lengthy a course of is working in Unix-like methods. However, how do you when did it finish and/or what’s the whole time taken by the command/course of to finish? Properly, It’s simple! On Unix-like methods, there’s a utility named ‘GNU time’ that’s particularly designed for this objective. Utilizing Time utility, we are able to simply measure the full execution time of a command or program in Linux working methods. Good factor is ‘time’ command comes preinstalled in most Linux distributions, so that you don’t need to trouble with set up.

Discover The Execution Time Of A Command Or Course of In Linux

To measure the execution time of a command/program, simply run.

$ /usr/bin/time -p ls


$ time ls

Pattern output:

dir1 dir2 file1 file2 mcelog

actual 0m0.007s
person 0m0.001s
sys 0m0.004s
$ time ls -a
. .bash_logout dir1 file2 mcelog .sudo_as_admin_successful
.. .bashrc dir2 .gnupg .profile .wget-hsts
.bash_history .cache file1 .native .stack

actual 0m0.008s
person 0m0.001s
sys 0m0.005s

The above instructions shows the full execution time of ‘ls’ command. Exchange “ls” with any command/means of your alternative to search out the full execution time.

Right here,

actual -refers the full time taken by command/program,
person – refers the time taken by this system in person mode,
sys – refers the time taken by this system in kernel mode.

We will additionally restrict the command to run just for a sure time as nicely. Refer the next information for extra particulars.

time vs /usr/bin/time

As you might observed, we used two instructions ‘time’ and ‘/usr/bin/time’ within the above examples. So, you may marvel what’s the distinction between them.

First, allow us to see what truly ‘time’ is utilizing ‘sort’ command. For many who don’t know, the Sort command is used to search out out the details about a Linux command. For extra particulars, refer this information.

$ sort -a time
time is a shell key phrase
time is /usr/bin/time

As you see within the above output, time is each,

A key phrase constructed into the BASH shell
An executable file i.e /usr/bin/time

Since shell key phrases take priority over executable recordsdata, once you simply runtimecommand with out full path, you run a built-in shell command. However, If you run /usr/bin/time, you run an actual GNU time program. So, so as to entry the actual command, you might must specify its specific path. Clear, good?

The built-in ‘time’ shell key phrase is on the market in most shells like BASH, ZSH, CSH, KSH, TCSH and so on. The ‘time’ shell key phrase has much less choices than the executables. The one possibility you need to use in ‘time’ key phrase is -p.

You realize now how you can discover the full execution time of a given command/course of utilizing ‘time’ command. Need to know little bit extra about ‘GNU time’ utility? Learn on!

A short introduction about ‘GNU time’ program

The GNU time program runs a command/program with given arguments and summarizes the system useful resource utilization as normal output after the command is accomplished. Not like the ‘time’ key phrase, the GNU time program not simply shows the time utilized by the command/course of, but in addition different assets like reminiscence, I/O and IPC calls.

The standard syntax of the Time command is:

/usr/bin/time [options] command [arguments…]

The ‘choices’ within the above syntax refers a set of flags that can be utilized with time command to carry out a selected performance. The record of accessible choices are given under.

-f, –format – Use this feature to specify the format of output as you would like.
-p, –portability – Use the moveable output format.
-o file, –output=FILE – Writes the output to FILE as an alternative of displaying as normal output.
-a, –append – Append the output to the FILE as an alternative of overwriting it.
-v, –verbose – This feature shows the detailed description of the output of the ‘time’ utility.
–quiet – This feature prevents the time ‘time’ utility to report the standing of this system.

When utilizing ‘GNU time’ program with none choices, you will notice output one thing like under.

$ /usr/bin/time wc /and so on/hosts
9 28 273 /and so on/hosts
zero.00person zero.00system zero:00.00elapsed 66%CPU (0avgtext+0avgdata 2024maxresident)okay
0inputs+0outputs (0major+73minor)pagefaults 0swaps

Should you run the identical command with the shell built-in key phrase ‘time’, the output can be bit completely different:

$ time wc /and so on/hosts
9 28 273 /and so on/hosts

actual 0m0.006s
person 0m0.001s
sys 0m0.004s

Some occasions, you may need to write the system useful resource utilization output to a file reasonably than displaying within the Terminal. To take action, use -o flag like under.

$ /usr/bin/time -o file.txt ls
dir1 dir2 file1 file2 file.txt mcelog

As you’ll be able to see within the output, Time utility doesn’t show the output. As a result of, we write the output to a file named file.txt. Allow us to take a look at this file:

$ cat file.txt
zero.00person zero.00system zero:00.00elapsed 66%CPU (0avgtext+0avgdata 2512maxresident)okay
0inputs+0outputs (0major+106minor)pagefaults 0swaps

If you use -o flag, if there is no such thing as a file named ‘file.txt’, it’s going to create and write the output in it. If the file.txt is already current, it’s going to overwrite its content material.

You can too append output to the file as an alternative of overwriting it utilizing -a flag.

$ /usr/bin/time -a file.txt ls

The -f flag permits the customers to regulate the format of the output as per his/her liking. Say for instance, the next command shows output of ‘ls’ command and reveals simply the person, system, and whole time.

$ /usr/bin/time -f “tpercentE actual,tpercentU person,tpercentS sys” ls
dir1 dir2 file1 file2 mcelog
zero:00.00 actual, zero.00 person, zero.00 sys

Please be aware that the built-in shell command ‘time’ doesn’t help all options of GNU time program.

For extra particulars about GNU time utility, refer the person pages.

$ man time

To know extra about Bash built-in ‘Time’ key phrase, run:

$ assist time

And, that’s all for now. Hope this handy.

Extra good stuffs to return. Keep tuned!



Thanks for stopping by!

Assist us that will help you:

Have a Good day!!

Source link

Related Articles

Back to top button