- The time command measures an application's execution time.
Consider the following example:
$ time APPLICATION
The time command executes APPLICATION. When APPLICATION is complete, the time command reports the real, system, and user time statistics to stderr and sends the APPLICATION's normal output to stdout.
$ time ls
test.txt
next.txt
real 0m0.008s
user 0m0.001s
sys 0m0.003s
- The -o option will write the time statistics to a file:
$ /usr/bin/time -o output.txt COMMAND
The filename must appear immediately after the -o flag.
The -a flag can be used with -o to append the time statistics to a file:
$ /usr/bin/time -a -o output.txt COMMAND
- The -f option specifies the statistics to report and the format for the output. A format string includes one or more parameters prefixed with a %. Format parameters include the following:
- Real time: %e
- User time: %U
- System time: %S
- System Page size: %Z
We can create a formatted output by combining these parameters with extra text:
$ /usr/bin/time -f "FORMAT STRING" COMMAND
Consider this example:
$ /usr/bin/time -f "Time: %U" -a -o timing.log uname
Linux
The %U parameter specifies user time.
The time command sends the target application's output to stdout and the time command output to stderr. We can redirect the output with a redirection operator (>) and redirect the time information output with the (2>) error redirection operator.
Consider the following example:
$ /usr/bin/time -f "Time: %U" uname> command_output.txt
2>time.log
$ cat time.log
Time: 0.00
$ cat command_output.txt
Linux
- The format command can report memory usage as well as timing information. The %M flag shows the maximum memory used in KB and %Z parameter causes the time command to report the system page size:
$ /usr/bin/time -f "Max: %M K\nPage size: %Z bytes" \
ls>
/dev/null
Max: 996 K
Page size: 4096 bytes
In this example, the output of the target application is unimportant, so the standard output is directed to /dev/null rather than being displayed.