How to Use the time Command on Linux

Linux PC with an open terminal windowFatmawati Achmad Zaenuri / Shutterstock.com

Want to know how long a process is running and much more? The Linux time command returns time statistics, which helps you better understand the resources used by your programs.

time has many parents

There are many Linux distributions and different Unix-like operating systems. Each of these has a default command shell. The most common default shell in modern Linux distributions is the bash shell. But there are many others, such as the Z shell (zsh) and the Korn shell (ksh).

All these shells integrate their own time control, either as integrated command or as reserved word. When you type time in a terminal window, the shell runs its internal command instead of using the GNU time binary supplied with your Linux distribution.

We want to use the GNU version of the time because it has more options and is more flexible.

What time will run?

You can check which version will run with the type command. type will tell you if the shell will handle your statement itself, with its internal routines, or pass it to the GNU binary.

in a terminal window, type the word type, a space, then the word time and press Enter.

typing time

type the time in a bash terminal window

We can see that in the bash shell, time is reserved. This means that Bash will use its default internaltime routines.

typing time

type the time in a zsh terminal window

In the Z (zsh) shell, the time being reserved, the internal shell routines will be used by default.

typing time

type the time in a Korn shell window

In the Korn shell, time is a keyword. An internal routine will be used instead of the GNU time command.

RELATED: What is ZSH and why should you use it instead of Bash?

Execute the GNU time command

If your Linux system shell has an internal time routine, you must be explicit if you want to use the GNU time binary. You must either:

Specify the full path to the binary, such as / usr / bin / time. Run the time command to find this path.
Use the command time.
Use a backslash as time.

output of the time command in a terminal window

The time command gives us the path to the binary.

We can test it using / usr / bin / time as a command to run the GNU binary. It works. We receive a response from the time command telling us that we have not provided any command line parameters to work on.

The time of order entry also works, and we get the same usage information per hour. The command command tells the shell to ignore the following command so that it is processed outside the shell.

The use of a character before the name of the command is identical to the use of a command before the name of the command.

The easiest way to ensure that you use the GNU time binary is to use the backslash option.

time
time

time and time in a terminal window

time calls the shell version of time. time uses the binary time.

Using the time command

Let the time for some programs. We use two programs called loop1 and loop2. They were created from loop1.c and loop2.c. They do not do anything useful apart from demonstrating the effects of a type of coding inefficiency.

That's loop1.c. The length of a string is required in both nested loops. The length is obtained in advance, outside the two nested loops.

#include "stdio.h"
#include "string.h"
#include "stdlib.h"

main int (int argc, char * argv[])
{
int i, j, len, count = 0;
char szString[]= "how geek-how-geek-how-geek-how-geek-how-geek-how-geek";

// gets the length of the string once, outside the loops
len = strlen (szString);

for (j = 0; j <500000; j ++) {

for (i = 0; i <len; i ++) {

if (szString[i] == – & # 39;)
count ++;
}
}

printf ("% d counted n", count);

output (0);

} // end of the hand

That's loop2.c. The length of the chain is obtained each time for each cycle of the outer loop. This inefficiency should appear in the timings.

#include "stdio.h"
#include "string.h"
#include "stdlib.h"

main int (int argc, char * argv[])
{
int i, j, count = 0;
char szString[]= "how geek-how-geek-how-geek-how-geek-how-geek-how-geek";

for (j = 0; j <500000; j ++) {

// get the length of the string each
// time triggering loops
for (i = 0; i <strlen (szString); i ++) {

if (szString[i] == – & # 39;)
count ++;
}
}

printf ("% d counted n", count);

output (0);

} // end of the hand

Let's run the loop1 program and use the time to measure its performance.

time ./loop1

temporal results for loop1 in a terminal window

Now let's do the same thing for loop2.

time ./loop2

timeout of loop2 in a terminal window

This gave us two sets of results, but they are in a really ugly format. We can do something about this later, but select some information in the results.

When programs are run, there are two run modes between which they can switch. These are called user mode and kernel mode.

In short, a user-mode process can not directly access the hardware or reference memory outside of its own allocation. To access such resources, the process must address requests to the kernel. If the kernel approves the request, the process goes into kernel mode until the requirement is satisfied. The process is then put back into execution in user mode.

The results for loop1 indicate that loop1 has passed 0.09 seconds in user mode. Either the time spent in kernel mode is zero, or the time spent in kernel mode is too low to be recorded once rounded. The total elapsed time was 0.1 seconds. loop1 has averaged 89% CPU time over the duration of its total elapsed time.

The inefficient loop2 program took three times longer to run. His total elapsed time is 0.3 seconds. The duration of user mode processing is 0.29 seconds. Nothing is registered for kernel mode. loop2 has averaged 96% CPU time for the duration of its execution.

Format the output

You can customize the output of the time with the help of a format string. The format string can contain text and format specifiers. The list of format specifiers can be found on the manual page for the time. Each of the format specifiers represents information.

When the string is printed, the format specifiers are replaced by the actual values ​​that they represent. For example, the format specifier for the CPU percentage is the letter P. To indicate at a given time that a format specifier is not just an ordinary letter, add a percent sign to it, such as% P. Let's use it in an example.

The -f (format string) option is used to indicate at the time that the following is a format string.

Our format string will print the characters "Program:" and the name of the program (as well as any command line parameters you pass to the program). The format specifier% C stands for "The name and command line arguments of the command being synchronized". N causes the output to move to the next line.

There are many format specifiers and they are case-sensitive. So be sure to enter them correctly when you do it yourself.

We will then print the characters "Total time:" followed by the value of the total elapsed time for this program execution (represented by% E).

We use n to give another new line. We will then print the characters "User Mode (s)", followed by the value of the CPU time passed in user mode, indicated by the% U.

We use n to give another new line. This time, we are preparing the time value of the kernel. We print the characters "Mode (s) of the kernel", followed by the format specifier for the CPU time spent in kernel mode, which corresponds to% S.

Finally, we will print the characters " nCPU:" to give us a new line and the title of this data value. The% P format specifier will give the average percentage of CPU time used by the timed process.

The entire format string is enclosed in quotation marks. We could have included t characters to put tabs in the output if we had been difficult on the alignment of values.

time -f "Program:% C nTotal time:% E nMode (s) user (s)% U nMode (s) kernel (s)% S nCPU:% P" ./loop1

Exit the format string for loop1 in a terminal window

Sending the output to a file

To keep track of the timing of the tests you have done, you can send the time output to a file. To do this, use the -o (output) option. The output of your program will always be displayed in the terminal window. Only the output of the time is redirected to the file.

We can rerun the test and save the output to the test_results.txt file as follows:

time -o test_results.txt -f "Program:% C nTotal time:% E n User mode (s)% U nMode (s) mode (s)% S nCPU:% P".
cat test_results.txt

Output the format string for loop1 to a file in a terminal window

The output of the loop1 program is displayed in the terminal window and the time results are entered in the test_results.txt file.

If you want to capture the next result set in the same file, you must use the -a (add) option as follows:

time -o test_results.txt -a -f "Program:% C nTotal time:% E n User mode (s)% U nMode (s) mode (s)% S nCPU:% P ".
cat test_results.txt

Output of the loop2 format string added to the file in a terminal window

We should now understand why we used the% C format specifier to include the program name in the output of the format string.

And we are out of time

The time command is probably useful for programmers and developers to adjust their code, but it is also useful for those who want to know more about what's going on under the hood every time you start a program.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.