Time command is useful when you want to determine the command execution time. Generally, it’s useful to test the performance of the commands and scripts. In this guide, we will cover how to use the time
command in Linux.
When you are writing multiple script which doing same job and want to choose better one by performance, you can use the time
command to test the execution time
of each script.
Time Command Versions
The most widely used Linux shells Bash and Zsh have their built-in versions of the time
command which is better than the Gnu time
command.
Use the type command to determine whether the time
is a binary or a built-in keyword.
type time
# Bash
time is a shell keyword
# Zsh
time is a reserved word
# GNU time (sh)
time is /usr/bin/time
In Gnu time
command, you need to specify the full path /usr/bin/time
to the time
binary. Use the env
command or use a leading backslash \time
which prevents both and built-ins from being used.
You can format the output and show other useful information like memory I/O and IPC calls using the Gnu time
.
Using Linux Time Command
Let’s see an example, to measure the time
of download the wordpress using the wget tool:
time wget https://wordpress.org/latest.tar.gz
The output is depends on the version of the time
command which your system have:
#Bash
real 0m3.565s
user 0m0.042s
sys 0m0.092s
# Zsh
0.042s user 0.092s system 1% cpu 3.565s total
# GNU time (sh)
0.042user 0.092system 0:03.57elapsed 1%CPU (0avgtext+0avgdata 6060maxresident)k
0inputs+201456outputs (0major+315minor)pagefaults 0swaps
Here,
- The real or total or elapsed is showing the
time
which taken from start towget
command completed. - user – amount of CPU
time
spent in user mode. - system or sys – It shows the amount of CPU
time
spent in kernel mode.
Conclusion
You successfully learned how to use the time
command. You can learn more about the Gnu time command by visiting the time man page.
If you have any question or feedback, please leave a comment below.