Check How Long a Command Takes to Run in Ubuntu
The time command allows you to check how long a command takes to complete in Ubuntu.
The time
command checks how long a command takes to run in Ubuntu. It can be used like this:
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
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.
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
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
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.
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>
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:
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 |
\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>
This could be a statement as well:
\time -f "the command %C takes up %E time with '%M' KB memory occupied" <command>
To print every information possible, there's a verbose mode just for that.
\time -v <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.
This is Pranav Krishna, a Proud Linux user in twelfth grade.