How to use the time command in Linux

Contents

Linux PC with an open terminal window

Do you want to know how long a procedure takes and much more? El Linux time The command returns time statistics, which gives you interesting information about the resources used by your programs.

time has many relatives

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

All of these shells incorporate their own time command, either as a Incorporated command or as a reserved word. When you write time in a terminal window, the shell will execute its internal command instead of using GNU time binary that is provided as part of your Linux distribution.

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

What time will it run?

You can check which version will run using the type command. type will let you know if the shell will handle your instructions on its own, with their internal routines, or it will convert them to the GNU binary.

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

type time

write time in a bash terminal window

We can see that in the bash shell time it's a reserved word. This means that Bash will use itstime default routines.

type time

write the time in a zsh terminal window

In the Z shell (zsh) time it's a reserved word, so internal shell routines will be used by default.

type time

type time into a korn shell window

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

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

Running the GNU time command

If your Linux system shell has a time routine will have to be explicit if you want to use the GNU time tracks. You should:

  • Provide the full path to the binary, What /usr/bin/time. Run the which time command to find this path.
  • Use command time.
  • Use a backslash like time.

the which time The command gives us the path to the binary.

We can test this using /usr/bin/time as a command to start the GNU binary. That works. We received a response from time command that tells us we don't provide any command line parameters for it to work.

Typing command time it also works, and we get the same usage information from time. the command command tells the shell to ignore the following command so that it is processed outside of the shell.

Using a character before the command name is the same as using command before the command name.

The easiest way to make sure you are using GNU time binary is to use the backslash option.

time
time

time invoke the shell weather version. time use the time tracks.

Using the time command

Let's measure some programs. We are using two programs called loop1 and loop2. They were created from loop1.c and loop2.c. They don't do anything useful other than demonstrate the effects of a kind of coding inefficiency.

This is loop1.c. The length of a string is needed within the two nested loops. The length is obtained in advance, outside the two nested loops.

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

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

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

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

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

  if (szString[i] == '-')
    count++;
   }
 }

 printf("Counted %d hyphensn", count);

 exit (0);

} // end of main

This is loop2.c. The length of the string is obtained again and again for each cycle of the outer loop. This inefficiency should be reflected in the times.

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

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

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

 // getting length of string every
 // time the loops trigger
 for (i=0; i < strlen(szString); i++ ) {

   if (szString[i] == '-')
    count++;
   }
 }

 printf("Counted %d hyphensn", count);

 exit (0);

} // end of main

Let's turn on the loop1 programming and using time to measure their performance.

time ./loop1

Now let's do the same for loop2.

time ./loop2

That has given us two sets of results, but they are in a truly disgusting format. We can do something about it later., but let's pick some bits of information from the results.

When programs run, there are two modes of execution between which they alternate. These are called user mode and modo kernel.

Briefly, a user-mode procedure cannot directly access hardware or reference memory outside of its own allocation. To get access to these resources, the procedure must make requests to the kernel. If the kernel approves the request, the procedure executes in kernel mode until the requirement is satisfied. Next, the procedure returns to user mode execution.

The results for loop1 tell us that loop1 step 0,09 seconds in user mode. Either zero time spent in kernel mode or time in kernel mode is too low to record once it has been rounded down. The total elapsed time was 0,1 seconds. loop1 received an average of 89% CPU time over total elapsed time.

The inefficient loop2 The program took three times longer to run. Your total elapsed time is 0,3 seconds. The duration of the processing time in user mode is 0,29 seconds. Nothing is logged for kernel mode. loop2 received an average of 96% CPU time while running.

Format the output

You can customize the output from time using a format string. Format string can contain text and format specifiers. The list of format specifiers can be found on the man page by time. Each of the format specifiers represents a piece of information.

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

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

Our format string will print the characters "Program:”And the name of the program (and any command line parameters that you pass to the program). the %C The format specifier means “Command-line name and arguments of the command being timed”. the n makes the output move to the next line.

There are many format specifiers and they are case sensitive, so make sure you enter them correctly when doing it yourself.

Next, we will 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. Next, we will print the characters “Way (s) of user”, followed by the value of CPU time spent in user mode, represented by the %U.

We use n to give another new line. This time we are preparing for the kernel time value. We print the characters “Kernel Mode (s)”, followed by the format specifier for the CPU time spent in kernel mode, What is it %S.

Finally, let's print the characters “nCPU: ”To give us a new line and title for this data value. the %P The format specifier will give the average percentage of CPU time used by the timed procedure.

The entire format string is in quotes. We could have included some t characters to place tabs in the output if we are picky about aligning values.

time -f "Program: %CnTotal time: %EnUser Mode (s) %UnKernel Mode (s) %SnCPU: %P" ./loop1

Send the output to a file

To keep a record of the times of the tests you have performed, you can send the output from time to a file. To do this use the -o (Exit) option. The output of your program will continue to be displayed in the terminal window. It's just the exit from time which is redirected to the file.

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

time -o test_results.txt -f "Program: %CnTotal time: %EnUser Mode (s) %UnKernel Mode (s) %SnCPU: %P" ./loop1
cat test_results.txt

the loop1 The output of the program is displayed in the terminal window and the results of time Go to the test_results.txt proceedings.

If you want to capture the next set of results in the same file, must use the -a (Add) next way option:

time -o test_results.txt -a -f "Program: %CnTotal time: %EnUser Mode (s) %UnKernel Mode (s) %SnCPU: %P" ./loop2
cat test_results.txt

It should now be apparent why we use the %C format specifier to include the program name in the output of the format string.

And we are out of time

Probably most useful for programmers and developers to fine-tune their code, the time The command is also useful for anyone who wants to find out a little more about what goes on under the hood every time they start a program..

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.