📅  最后修改于: 2023-12-03 14:48:06.425000             🧑  作者: Mango
As a programmer, working with the command line interface (CLI) is an essential part of our job. One of the most common tasks we perform on the CLI is measuring the time taken to execute a command or a script.
In this article, we will learn how to use the time
command on Ubuntu to measure the execution time of a shell script or a command. We will also cover some advanced features of the time
command, such as redirecting the output to a file and disabling the shell's built-in time
command.
The time
command is a built-in utility on Ubuntu, which is used to measure the duration of a program's execution. In its most basic form, you can run the time
command followed by a command or a script that you want to measure:
time command
For example, let's measure the time it takes to execute the ls
command, which lists the contents of a directory:
time ls
This will output the following information:
real 0m0.004s
user 0m0.004s
sys 0m0.000s
The output consists of three lines:
real
: This is the actual time taken by the program to execute, including any overhead time, such as setting up the environment, loading libraries, or writing output to the console.user
: This is the amount of CPU time used by the program, executing in user mode, without considering any system calls or kernel operations.sys
: This is the amount of CPU time used by the program, executing in kernel mode, performing system calls or other privileged operations.The time
command can also output the results to a file, instead of displaying them on the console. This is useful when you need to measure the execution time of a command or a script, but you do not want to clutter the console output.
To redirect the output of the time
command to a file, you can use the standard output redirection operator >
:
time command > output.log
For example, to measure the execution time of a script test.sh
and save the output to a file output.log
, you can use:
time ./test.sh > output.log
By default, Ubuntu's shell (Bash) has a built-in time
command that performs a similar function to the /usr/bin/time
command we have been using until now. However, the shell's time
command outputs timing information in a different format and does not provide as much detail as the time
command we have been using.
To disable the shell's built-in time
command and use the /usr/bin/time
command instead, you can prefix your command or script with the full path to the time
command, like so:
/usr/bin/time command
For example, to disable the shell's built-in time
command and measure the execution time of the ls
command, you can use:
/usr/bin/time ls
This will output timing information in the same format as before:
real 0m0.004s
user 0m0.004s
sys 0m0.000s
In this article, we learned how to use the time
command on Ubuntu to measure the execution time of a command or a script. We also covered some advanced features of the time
command, such as redirecting the output to a file and disabling the shell's built-in time
command.
By mastering the time
command, you can optimize your programs and scripts, and identify performance bottlenecks.