This tutorial explains Linux “time” command, options and its usage with examples.
DESCRIPTION
The time command runs the specified program command with the given arguments. When command finishes, time writes a message to standard output giving timing statistics about this program run.
The utility ‘time’ takes a program name as an input and displays information about the resources used by the program. Also, if the command exists with non-zero status, this utility displays a warning message and exit status.
SYNOPSIS
time [options] command [arguments…]
OPTIONS
-f FORMAT, –format=FORMAT
Specify output format, possibly overriding the format specified in the environment variable TIME.
-p, –portability
Use the portable output format.
When the -p option is given the (portable) output format
real %e
user %U
sys %S
is used.
-o FILE, –output=FILE
Do not send the results to stderr, but overwrite the specified file.
-a, –append
(Used together with -o.) Do not overwrite but append.
-v, –verbose
Give very verbose output about all the program knows about.
The format string
The format is interpreted in the usual printf-like way. Ordinary characters are directly copied, tab, newline and backslash are escaped using \t, \n and \\, a percent sign is represented by %%, and otherwise % indicates a conversion. The program time will always add a trailing newline itself. The conversions follow.
Time
%E
Elapsed real time (in [hours:]minutes:seconds).
%e
(Not in tcsh.) Elapsed real time (in seconds).
%S
Total number of CPU-seconds that the process spent in kernel mode.
%U
Total number of CPU-seconds that the process spent in user mode.
%P
Percentage of the CPU that this job got, computed as (%U + %S) / %E.
Memory
%M
Maximum resident set size of the process during its lifetime, in Kbytes.
%t
(Not in tcsh.) Average resident set size of the process, in Kbytes.
%K
Average total (data+stack+text) memory use of the process, in Kbytes.
%D
Average size of the process’s unshared data area, in Kbytes.
%p
(Not in tcsh.) Average size of the process’s unshared stack space, in Kbytes.
%X
Average size of the process’s shared text space, in Kbytes.
%Z
(Not in tcsh.) System’s page size, in bytes. This is a per-system constant, but varies between systems.
%F
Number of major page faults that occurred while the process was running. These are faults where the page has to be read in from disk.
%R
Number of minor, or recoverable, page faults. These are faults for pages that are not valid but which have not yet been claimed by other virtual pages. Thus the data in the page is still valid but the system tables must be updated.
%W
Number of times the process was swapped out of main memory.
%c
Number of times the process was context-switched involuntarily (because the time slice expired).
%w
Number of waits: times that the program was context-switched voluntarily, for instance while waiting for an I/O operation to complete.
I/O
%I
Number of file system inputs by the process.
%O
Number of file system outputs by the process.
%r
Number of socket messages received by the process.
%s
Number of socket messages sent by the process.
%k
Number of signals delivered to the process.
%C
(Not in tcsh.) Name and command line arguments of the command being timed.
%x
(Not in tcsh.) Exit status of the command.
EXAMPLES
1. Simple Example
Displaying resources used by ls command.
$ /usr/bin/time ls abc Desktop examples.desktop Music Templates abc.txt Documents file1.txt Pictures Ubuntu One abv Downloads file2.txt Public Videos 0.00user 0.00system 0:00.00elapsed 0%CPU (0avgtext+0avgdata 992maxresident)k 0inputs+0outputs (0major+310minor)pagefaults 0swaps
2. Using a format
Displaying resources used in a specific format by ls command. For format see above.
$ /usr/bin/time -f "\t%U user,\t%S system,\t%x status" ls abc Desktop examples.desktop Music Templates abc.txt Documents file1.txt Pictures Ubuntu One abv Downloads file2.txt Public Videos 0.00 user, 0.00 system, 0 status
NOTE : Why /usr/bin/time? (Instead of just time)
Lets not use /usr/bin/time and use ‘time’ instead.
$ time -f "\t%U user,\t%S system,\t%x status" date -f: command not found real 0m0.255s user 0m0.230s sys 0m0.030s
As seen from the output above, the ‘time’ command when used without the complete path (/usr/bin/time) spits out an error regarding the ‘-f’ flag. When ‘time’ command is executed without the complete path (/usr/bin/time), then its the built-in ‘time’ command of the bash shell that is executed.
3. Write Time Statistics Output to a File using -o option
The -o option is used for directing the output to a file.
$ /usr/bin/time -o time.txt sleep 2 $ cat time.txt 0.00user 0.00system 0:02.00elapsed 0%CPU (0avgtext+0avgdata 2288maxresident)k 0inputs+0outputs (0major+175minor)pagefaults 0swaps
4. Display Percentage of CPU used
Displaying % of CPU used according to its format and searching the program using ‘find’.
$ /usr/bin/time -f "\t%P CPU Percentage" find / -name my-program.sh /root/my-program.sh 82% CPU Percentage
5. Display Total Number of CPU-seconds
Displaying Total number of CPU seconds according to its format and searching the program using ‘find’.
$ /usr/bin/time -f "\t%S CPU-seconds" find / -name my-program.sh /root/my-program.sh 0.35 CPU-seconds
6. Display Elapsed Real Time in Seconds – %e
This option gives elapsed real time (i.e. wall clock) used by the process, in seconds.
$ /usr/bin/time -f "\t%e Elapsed Real Time (secs)" sleep 2 2.00 Elapsed Real Time (secs)
Here, command output shows that sleep command execution elapsed till 2 seconds.
7. Display Program Name and Command Line Arguments
Displaying Program Name and Command Line Arguments according to its format and searching the program using ‘find’.
$ /usr/bin/time -f "\t%C (Program Name and Command Line)" find / -name my-program.sh /root/my-program.sh find / -name my-program.sh test_time (Program Name and Command Line)
8. Display System Page Size in Bytes
%z format is used for the System Page Size.
$ /usr/bin/time -f "\t%Z System Page Size (bytes)" sleep 2 4096 System Page Size (bytes)
9. Display Number of Context Switches
%c format is used for Context Switches.
$ /usr/bin/time -f "\t%c Context Switches" find / -name my-program.sh /root/my-program.sh 254 Context Switches
10. Display Exit Status of a Command
%x is used to display exit status.
$ /usr/bin/time -f "\t%x Exit Status" top12 /usr/bin/time: cannot run top12: No such file or directory 127 Exit Status
Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.
- Apply for Linux Internship
- Apply for Programming Internship
- Buy Linux Books
- Buy Information Technology Books
- Practice Programming MCQs