The time program is a handy tool to not only guage how much time in seconds it takes a program to run, but will also display how much user CPU time and system CPU time was used to execute the process. To understand these values you must grasp how the kernel handles the time reporting for the process. For example, the output of:
The first line, real, is how much real time or “wall-clock” time the process took to execute. In this case, 0.007 seconds or micro-seconds. The second line, user, is how much user time the CPU used. User time is amount of time the CPU spends performing an action for a program that is not a system call. The third value, “sys” referred to as system CPU time, is the amount of time the CPU spends peforming system calls on behalf of the program. A system call is a request made to the Kernel by the program. Therefore, CPU time is the total of user time plus system time. CPU time will not necessarily equal real (wall-clock) time, and if it does not total CPU time would be less than wall-clock time. There can be many reasons for the discrepancy between wall-clock and CPU time including the process waiting for another process to complete. In the example above total CPU time is 0.003 seconds where as wall-clock time is 0.007s.
Most Linux distributions default to using the Bash shell. Bash has a built in time function that operates as listed above and is quite different than the actual time command. The functionality of the bash time command is very limited to the actual time command. The remainder of this article will focus on the actual time command as opposed to the built in bash time command.
Most distributions have the time command installed under /usr/bin. Executing:
Should output the path to the time command:
If no value is returned chances are you do not have the time command installed (I had this issue on Arch and had to install the time command by sudo pacman -S time, on Slackware it was already installed).
To use the time command as opposed to the bash built in command you must provide the full path to time:
Note that the output is quite different:
0.00user 0.00system 0:00.00elapsed 50%CPU \
0inputs+0outputs (0major+250minor)pagefaults 0swaps
There is a lot more information provided compared to the builtin Bash time command.
The first three items: 0.00user 0.00system 0:00.00elapsed should look familiar. These values are user CPU time, system CPU time, and wall-clock time respectively. To understand the remaining values better focus will be directed on how to actually format the output of the time command. This is done with the -f, or –format=, switch followed by the formatting strings. Similar to the date command, the formatting strings are in the form of “%” + variable:
- %E = Elapsed wall-clock time in [hours:]minutes:seconds – note hours will not show unless the process does take over an hour
- %e = Elapsed wall-clock time in seconds only (this option is not available in tcsh).
- %S = system CPU time time in seconds
- %U = user CPU time in seconds
- %P = Percentage of CPU that was given to the process – equation is (%U + %S) / %E
- %M = Maximum resident set size of the process during its lifetime in KB. This value is the maximum amount of physical memory (RAM) this process utilized during the life of the process.
- %t = Average resident size of the process in Kilo Bytes. This is the average amount of physical RAM the process utilized over its lifetime (not available in tcsh).
- %K = Average total (data + stack + text) memory used by the process in Kilo Bytes. In a nut shell
- data = Global data, variables, etc.
- stack = Where variables are declared and initialized
- text = The actual programs
- %D = Average size of the processes unshared data area in Kilo Bytes – This is where data for the process is stored.
- %p = Average size of the processes unshared stack space in Kilo Bytes – This is where variables are declared and initialized (not in tcsh)
- %X = Average size of the processes unsahred text space in Kilo Bytes – This is where the actual program resides
- %Z = System’s page size – This is the size of a single block of contiguous virtual memory (likely to be 4096 bytes) (not in tcsh)
- %F = Number of major page faults that occurred while the process was running. A major page fault is when the process attempts to access a page that is mapped in the virtual address space but is not available in physical memory, it needs to find a space in physical memory to map the page.
- %R = Number of minor page faults that occurred while the process was running – Similar to a major fault, a minor page fault is when the page is actually loaded in memory at the time but is not marked in the memory management unit as being loaded in memory. The operating system needs to mark the page as loaded in the memory management unit before it can be loaded.
- %W = The number of times the process was swapped out of main memory
- %c = The number of times the process was context-switched involuntarily. In a multi-processing environment a process may need to be switched out for another process that needs the CPU. The CPU state is saved so the process can be resumed. Involuntary swapping can be caused by the application being forced to swap out because it’s time slice (a lotted time to execute) was exceeded.
- %w = The number of waits that the program was context-switched voluntariliy. A volunary swap may be because the process was waiting for another process or I/O action to complete before it could continue.
- %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 = Name and command-line arguements of the command being timed (not in tcsh).
- %x = Exit status of the command (not in tcsh).
Now that these values have been defined the output of:
From above can be further explained:
0.00user 0.00system 0:00.00elapsed 50%CPU (0avgtext+0avgdata 784maxresident)k 0inputs+0outputs (0major+250minor)pagefaults 0swaps
In a nutshell:
%U %S %E %P (%X + %D %M)k
%I + %O (%F+$R)pagefaults %W
(User CPU Time) (System CPU Time) (Wall-Clock Time) ( (Average size of shared text space) + (Average size of unsahred data are) (Maximum Resident Set Size of Process) )k
(Number of File System Inputs) + (Number of File System Outputs) ( (Number of Major Page Faults) + (Number of Minor Page Faults) pagefaults (Number of times process was swapped out of memeory)
As stated, formatting of the /usr/bin/time command can be done with -f, or –format=, switch followed by a string including the above variables:
/usr/bin/time -f “%e may not equal %S + %U” ls
The output of this may look something like:
0.04 may not equal 0.00 + 0.00
To emulate something like the output of the Bash built in time command you could execute:
/usr/bin/time -f “real\t%e \nuser\t%U \nsys\t%S” ls
The output may look like:
Or you could pass the -p, –portability, switch which provides the same formatting:
The output of time can be directed to a file with the -o, or –output=, switch and supplying the file name:
/usr/bin/time -o times.txt ls
This would redirect the output of the /usr/bin/command to the file times.txt. If you executed the same command again, it would overwrite the original times.txt file. If you want to preserve the original contents of the times.txt file use the -a, or –append, switch with the -o, –output=, switch:
/usr/bin/time -o times.txt -a ls
The final switch to discuss is the -v, or –verbose, switch which will output all the values possible that /usr/bin/time can report regarding a process.
/usr/bin/time -v tar cvf test.tar.gz lib
Produces the following output:
That is the time and /usr/bin/time commands in a nutshell. Remember, if you want the full feature set of the time command you must provide the full path to the time command. Otherwise you will more than likely use the built in time command of the shell you are running.
- man time
- Wikipedia page on Unix Time
- Wikipedia page on System Call
- Wikipedia Page on Resident Set Size
- Data and Text Segments
- Memory Layout of a C Program
- Wikipedia Page on Page Faults
- Wikipedia Page on Context Switching
- Time Command Examples
Video failed youtube processing, will be up shortly, use archive.org link below.
If the video is not clear enough view it off the YouTube website and select size 2 or full screen. Or download the video in Ogg Theora format:
- Episode 024 – time Ogg Theora Video – Archive.org
Thank you very much!