Get Return Time Statistics With the Linux Time Command

Display return-time statistics to fine-tune the performance of programs

The Linux Time Command on desktop

Lifewire / Gary Newell

The time command is one of the lesser-known Linux commands. It shows how long a command takes to run. It's most useful for developers testing the performance of a program or script.

How to Use the Time Command

linux time command

The syntax of the time command is as follows:

time

For example, run the ls command to list all the files in a folder in a long format along with the time command.

time ls -l

The results from the time command will be as follows:

real 0m0.177s
user 0m0.156s
sys 0m0.020s

The statistics shown show the total time is taken to run the command, the amount of time that was spent in user mode and the amount of time spent in kernel mode.

By default, the output is displayed at the end of the program. To output the format to a file, use the following syntax:

time -o
time --output=

All of the switches for the time command must be specified before the command you wish to run.

If you are performance tuning, append the output from the time command to the same file over and over so that you can see a trend. Use the following syntax instead:

time -a
time --append

Formatting the Output of the Time Command

By default the output is as follows:

real 0m0.177s
user 0m0.156s
sys 0m0.020s

Modify the performance of the command using any of the following switches:

  • -C: Name and command line arguments used
  • -D: Average size of the process's unshared data area in kilobytes
  • -E: Elapsed time in a clock format
  • -F: Number of page faults
  • -I: Number of file system inputs by the process
  • -K: Average total memory use of the process in kilobytes
  • -M: Maximum resident set size of the process during the lifetime in Kilobytes
  • -O: Number of file system outputs by the process
  • -P: Percentage of CPU that the job received
  • -R: Number of minor or recoverable page faults
  • -S: Total number of CPU seconds used by the system in kernel mode
  • -U: Total number of CPU seconds used by user mode
  • -W: Number of times the process was swapped out of main memory
  • -X: Average amount of shared text in the process
  • -Z: System's page size in kilobytes
  • -c: Number of times the process was context-switched
  • -e: Elapsed real-time used by the process in seconds
  • -k: Number of signals delivered to the process
  • -p: Average unshared stack size of the process in kilobytes
  • -r: Number of socket messages received by the process
  • -s: Number of socket messages sent by the process
  • -t: Average resident set size of the process in kilobytes
  • -w: Number of time the process was context-switched voluntarily
  • -x: Exit status of the command

Use the formatting switches as follows:

time -f "Elapsed Time = %E, Inputs %I, Outputs %O"

The output for the above command would be something like this:

Elapsed Time = 0:01:00, Inputs 2, Outputs 1

Mix and match the switches as required.

To add a new line as part of the format string use the newline character as follows:

time -f "Elapsed Time = %E \n Inputs %I \n Outputs %O"