How to use the time command on Linux

Would you like to know how long a process will run and much more? The Linux time Command returns time statistics that give you a cool idea of ​​the resources your programs are using.

Zeit has many relatives

There are many Linux distributions and various Unix-like operating systems. Each of them has a standard command shell. The most common standard shell in modern Linux distributions is the bash Sleeve. But there are many others like the Z shell (zsh) and the Korn shell (ksh).

All of these bowls contain their own time Command, either as a built-in Command or as a reserved word. When you type time in a terminal window, instead of using the GNU, the shell executes its internal command time Binary file provided as part of your Linux distribution.

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

What time is running?

You can check which version is running by checking the type Command. type will let you know whether the shell is processing your statement itself using its internal routines or is passing it to the GNU binary.


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

type time

We see that in the bash Sleeve time is a reserved word. That means bash will be internal. usedtime Routines by default.

type time

Enter time in a ZSH terminal window

In the Z shell (zsh) time is a reserved word, so the internal shell routines are used by default.

type time

Type time in a Korn shell window

In the Korn Shell time is a keyword. Instead of GNU. an internal routine is used time Command.

What is ZSH and why would you use it instead of Bash?

Executing the GNU time command

If the shell of your Linux system has an internal time Routine must be explicitly to use the GNU time binary. You must either:

  • Enter the full path to the binary file, e.g. B. /usr/bin/time. Run the which time Command to find this path.
  • Use command time.
  • Use a backslash like time.

the which time Command gives us the path to the binary file.


We can test this by using /usr/bin/time as a command to start the GNU binary. It is working. We get an answer from time Command that tells us that we haven’t specified any command line parameters to work on.

Tap command time works too, and we get the same usage information from time. the command Command tells the shell to ignore the next command so that it is processed outside of the shell.

Using a Character in front of the command name is the same as with command before the command name.

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

time
time

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

Using the time command

Let’s time some programs. We use 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 some kind of coding inefficiency.


This is loop1.c. The length of a character string is required within the two nested loops. The length is determined 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 determined again and again for each cycle of the outer loop. This inefficiency should show in the timings.

#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 light them up loop1 program and use time measure one’s performance.

time ./loop1

Now let’s do the same for loop2.

time ./loop2

That gave us two sets of results, but they’re in a really ugly format. We can do something about it later, but let’s pick some information from the results.

When programs are executed, there are two modes of execution that they are toggled between. These are known as user mode and kernel mode.

In short, a user-mode process cannot directly access hardware or reference storage outside of its own allocation. To gain access to such resources, the process must make requests to the kernel. If the kernel approves the request, the process will go into kernel mode execution until the request is met. The process is then switched back to user mode execution.


The results for loop1 tell us that loop1 Spent 0.09 seconds in user mode. It has either spent zero time in kernel mode or the time in kernel mode is too little to be registered after rounding down. The total elapsed time was 0.1 seconds. loop1 was allotted an average of 89% of CPU time over the total elapsed time.

The inefficient one loop2 The program took three times longer to run. The total elapsed time is 0.3 seconds. The processing time in user mode is 0.29 seconds. Nothing registers for kernel mode. loop2 Received an average of 96% of CPU time for the duration of its execution.

Format the output

You can customize the output of time with a format string. The format string can contain text and format specifiers. The list of format specifiers can be found on the manpage to the time. Each of the format designators represents information.

When the string is printed, the format specifiers are replaced with the actual values ​​they represent. To the example, the format specifier for the percentage of CPU is the letter P . To indicate that time that a format specifier is not just a normal letter, add a percent sign, like %P . Let’s use it in one example.

the -f (Format-String) option is used to say time what follows is a format string.

Our format string prints the characters “program:” and the name of the program (and any command line parameters you pass to the program). the %C The format specifier stands for “name and command line arguments of the scheduled command”. the n causes the output to be moved to the next line.


There are many case-sensitive format specifiers. So make sure to type them correctly if you do this yourself.

Next we print the characters “Total Time:” followed by the value of the total elapsed time for this program run (represented by %E).

We use n to give another new line. We then print out the characters “user mode (s)” followed by the value of CPU time spent in user mode, indicated by the %U.

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

Finally we print the characters “nCPU: ”to give us a new line and the title for this data value. the %P The format specifier indicates the average percentage of CPU time used by the scheduled process.

The entire format string is enclosed in quotation marks. We could have included a few t Characters to put tabs in the output when we were picky about aligning the values.

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

Send the output to a file

To record the times of the tests you performed, you can use the output of. send time to a file. Use the -o (Output) option. The output of your program will still be displayed in the terminal window. It’s just the output of time that will be redirected to the file.


We can run the test again and see the output in 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 Program output is displayed in the terminal window and the results of time go to test_results.txt File.

If you want to collect the next set of results in the same file, you need to use the -a (append) option as follows:

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 be clear by now why we’re doing the %C Format specifier to include the name of the program in the format string output.

And we don’t have time

Probably most useful for programmers and developers for fine-tuning their code, the time The command is also useful for anyone who wants to learn more about what goes on under the hood every time a program starts.

Linux commands
Filestar · pv · cat · tac · chmod · grep · difference · sed · With · man · pushed · popd · fsck · Test disk · seq · fd · pandoc · CD · $ PATH · awk · join · jq · wrinkles · unique · Journalctl · tail · stat · ls · fstab · echo · fewer · chgrp · chown · rev · look · Strings · Type · rename · Postal code · unzip · assemble · ummount · To install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · weather · Nano · mkdir · from · ln · Patch · Convert · rclon · Scraps · srm
Processesalias · screen · above · kind · renice · progress · strace · system · tmux · chsh · story · at · Batch · for free · which · dmesg · chfn · User mod · ps · chroot · xargs · tty · pinkie finger · lsof · vmstat · Time out · Wall · Yes sir · kill · sleep · sudo · it is · Time · groupadd · User mod · groups · lshw · switch off · start anew · Stop · switch off · passwd · lscpu · crontab · date · bg · fg
Networkingnetstat · Ring · Trace route · ip · ss · who is · fail2ban · bmon · she · finger · nmap · ftp · curl · wget · who · who am I · w · iptables · ssh-keygen · ufw

Best Linux Laptops for Developers and Enthusiasts

Related Posts