How do I find the execution time of a command or process in Linux

  • 2020-12-19 21:23:08
  • OfStack

In a system like Unix, you might know when a command or process started to execute, and how long a process has been running. But how do you know when the command or process will end or how long it will take to complete its run? In an Unix-like system, this is very easy! There is a program specifically designed for this purpose called GNU time. Using the time program, we can easily measure the total execution time of commands or programs in the Linux operating system. The time command comes pre-installed in most Linux distributions, so you don't have to install it.

Find the execution time of a command or process in Linux

To measure the execution time of a command or program, run:

$ /usr/bin/time -p ls

Or,

$ time ls

Output sample:


dir1 dir2 file1 file2 mcelog
real 0m0.007s
user 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 .local .stack
real 0m0.008s
user 0m0.001s
sys 0m0.005s

The above command shows the total execution time of the ls command. You can replace ls with any command or process to find the total execution time.

Output details:

real - Total time spent by a command or program user - refers to the time spent by the program in user mode sys - refers to the time spent in kernel mode

We can also limit the command to run for only 1 period of time. See the following tutorial for more details:

How do you get a command to run for a certain amount of time in Linux

time with/usr/bin/time

As you may have noticed, we used the two commands time and /usr/bin/time in the above example. So, you might want to know the difference.

First, let's use the type command to see what the time command is. For those Linux commands that we don't know about, the type command is used to find information about the relevant commands. See this guide for more details.


$ type -a time
time is a shell keyword
time is /usr/bin/time

As you can see in the output above, time is two things:

One is a keyword built into BASH shell One is an executable, such as /usr/bin/time

Since the shell keyword has a higher priority than the executable, when you run the time command without giving the full path, you are running the command built into shell. However, when you run /usr/bin/time, you are running the real GNU time command. Therefore, in order to execute the actual command you may need to give the full path.

In most shell such as BASH, ZSH, CSH, KSH, TCSH, etc., the built-in keyword time is available. The time keyword has fewer options than the executable. The only option you can use is -p.

You now know how to use the time command to find the total execution time of a given command or process. Want to take a step closer to the GNU time tool? Keep reading!

A brief introduction to the GNU time program

The GNU time program runs a command or program with a given parameter and summarizes the system resource usage to standard output when the command is complete. Unlike the time keyword, the GNU time program displays not only the execution time of a command or process, but also other resources such as memory, I/O and IPC calls.

The syntax of the time command is:


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

options in the above syntax refers to a set of options that can be used with time command 1 to perform a specific function. The available options are given below:

-ES117en, format -- Use this option to specify the output format as required. -p, WES120en - use a concise output format. -ES121en file, �output=FILE - writes output to the specified file instead of to standard output. -ES125en, append - appends the output to the file instead of overwriting it. -ES127en, verbose - This option displays the details of the output from the time command. The quiet option prevents the time command from reporting the status of the program.

When using the GNU time command without any options, you will see the following output.


$ /usr/bin/time wc /etc/hosts
9 28 273 /etc/hosts
0.00user 0.00system 0:00.00elapsed 66%CPU (0avgtext+0avgdata 2024maxresident)k
0inputs+0outputs (0major+73minor)pagefaults 0swaps

If you run the same command using the shell keyword time, the output will be a little different:


$ time wc /etc/hosts
9 28 273 /etc/hosts
real 0m0.006s
user 0m0.001s
sys 0m0.004s

Sometimes, you may want to output system resource usage to a file instead of a terminal. To do this, you can use the -ES143en option, as shown below.


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

As you can see, the time command does not appear on the terminal. Because we wrote the output to the file file.txt. Let's take a look at the contents of this file:


$ cat file.txt
0.00user 0.00system 0:00.00elapsed 66%CPU (0avgtext+0avgdata 2512maxresident)k
0inputs+0outputs (0major+106minor)pagefaults 0swaps

When you use the -ES153en option, if you don't have a file named file.txt, it creates one and writes the output to it. If the file exists, it overwrites the original contents of the file.

You can use the -ES158en option to append the output to the end of the file instead of overwriting its contents.

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

The -f option allows users to control the output format according to their preferences. For example, the output of the following command shows only the user, system, and total time.


$ /usr/bin/time -f "\t%E real,\t%U user,\t%S sys" ls
dir1 dir2 file1 file2 mcelog
0:00.00 real, 0.00 user, 0.00 sys

Note that the time command built into shell does not have all the features of the GNU time program.

Detailed instructions on the GNU time program can be viewed using the man command.

$ man time

To learn more about the time built-in time keyword, run:

$ help time

conclusion


Related articles: