Check how long a command runs in Ubuntu
How To

Check How Long a Command Takes to Run in Ubuntu

Pranav Krishna
Pranav Krishna

Table of Contents

The time command checks how long a command takes to run in Ubuntu. It can be used like this:

time <command>
Syntax (<command> is to be replaced)

Example: Checking how long refreshing repos takes

For instance, to check how long updating the repositories takes in Ubuntu, I used the command like this:

time sudo apt update
Example of time command - Time taken for refreshing repositories (2.9s)
Time taken for refreshing repositories (2.9s) [click to expand]

The 'real' tag is precisely how long a command took to run. In this case, it's 2.9 seconds. user is the time spent in the user space and sys is the time spent in kernel.

Let's take a look in detail.

đź“‹
Do not associate the time command with displaying the current time from the clock. The date command does that job, whilst the time command checks how long any command takes to execute.

The time command

The time command is used to check how long a command takes to complete its execution. There are multiple versions of this command (the bash version, GNU version, etc).

To check what versions of time are installed in your system, you can list all types of the command time (using the type command)

type -a time
All types of time (zsh and bash)
All available types of time (shown in zsh and bash)

Here, the shell keyword and reserved word belong to Bash and Zsh (respectively), they're modified versions of this command mostly with limited functionality. But, /usr/bin/time and /bin/time refer to the GNU version of this command.

The bash version of 'time'

To use the bash version of the command, you just have to type 'time' in the shell.

time <command>

This provides the basic functionality of just checking how long the processor spent time on that particular command.

There are other examples. To check how long you spend time in your text editor:

time vim 
Checking how long a program is being run with time command
Checking how long a program is being run with time command

I was inside the editor for 17.8 seconds. You can track how long something takes to run like this. This includes GUI applications as well as long as you execute it from the command line.

Did you notice three different categories in the output? I'll explain each one.  

  • real - This is the entire time taken by the program from the start to the end of execution. If the program was waiting for something (like superuser permission), it will include that too.
  • user - It's the time spent in the user space, making use of the libraries and programs specific to the user. This excludes time spent in the kernel.
  • sys - The amount of time spent in the kernel due to the process.

The sum of user and sys will give out how much CPU time is spent on the process.

In a related post, you may want to learn about the sleep command.

Sleep Command in Ubuntu: How to Use it?
Here are a few practical examples of using the sleep command in Ubuntu Linux.

The 'GNU' version of time

The GNU version of the time command has more functionality as said before. It gives a detailed view of the time taken along with the amount of system resources used.

How to access it? Add a backslash (escape sequence) \ at the beginning of the time command or just type out the whole path (/usr/bin/time) to access the GNU variant.

/usr/bin/time <command>

OR

\time <command>
Output of GNU time running a command
The output of GNU time running a command

The time spent in user/system spaces, total time elapsed, CPU and memory usage, etc are displayed in this GNU time variant. This has more useful information than the shell version.

The bash's version of time can be given as an alias to the GNU time command with an option -p. You can note the similarities here:

Bash's time vs GNU time
Bash's time vs GNU time

Formatting output with GNU time

The good thing about the GNU time command is, you can customise the format in which the output is printed. This will be very useful during debugging. Here are a few most-used tags to check out:

Literal Tag Explanation
%E Elapsed time in minutes:seconds (and hours sometimes)
%e Elapsed time in seconds
%S Time spent in Kernel mode (aka sys)
%U Time spent in User mode
%P Percentage of 'time' in CPU (spent in User+Sys modes)
%M Total memory (in KB) used during the process
%C The command executed
%x Exit code of the command
đź’ˇ
To format the output, use this syntax:
\time -f "<format>" <command>, where <format> can be any text containing specific literal signs (like %e, %P, and more).

There are other tags of course, but the above listed are the popular ones.

Let us meet some examples:

\time -f "real %E user %U sys %S" <command>
Example demonstrating how output is printed (using the given tags)
Example using the 'real', 'user', and 'sys' times

This could be a statement as well:

\time -f "the command %C takes up %E time with '%M' KB memory occupied" <command>
Example using the 'command', 'memory' and 'real time' tags

To print every information possible, there's a verbose mode just for that.

\time -v <command>
Verbose mode in time command
Verbose mode in time command

Conclusion

The time command is a versatile one commonly used in debugging and benchmarking. An interesting fact is that modifying the system time by a few hours modifies the output of bash version of time, but it does not affect the GNU version.

For a quick check of how long a program takes to run, the bash's version of time will do the job. It gives out a short overview of how long.

The GNU's version of time command is more advanced and robust with the ability to be customised. This can be used to run long tests or benchmarking.

I hope you found this article helpful. If you know other tricks with the time command, make sure to drop them in the comments section.



Pranav Krishna

Pranav Krishna

This is Pranav Krishna, a Proud Linux user in twelfth grade.