Time command output

time

I have a bash script file called readspeed.

I am trying to find how long it takes time to execute a bash script file.

When I run time readspeed, the following is the output.

$ time readspeed                       
6
readspeed  174.19s user 286.30s system 99% cpu 7:40.50 total

I'm not sure what user, system, cpu, and total indicated.

What are the differences between user, system, and total?

$ type time
time is a reserved word

$ echo $SHELL
/bin/zsh

Best Answer

The output of echo $SHELL shows that you're running zsh - not bash. zsh is the default shell in recent versions of macOS. The time command is then actually performed by the shell instead of running a seperate time program (but it is funnily enough not listed as a shell built-in). The rather terse documentation is available by running man zshmisc:

time [ pipeline ] The pipeline is executed, and timing statistics are reported on the standard error in the form specified by the TIMEFMT parameter. If pipeline is omitted, print statistics about the shell process and its children.

The meaning of the TIMEFMT parameter can be found by running man zshparam:

   TIMEFMT
          The format of process time reports with the time keyword.  The default is `%J  %U user %S system %P cpu %*E total'. 

Recognizes the following escape sequences, although not all may be available on all systems, and some that are available may not be useful:

          %%     A `%'.
          %U     CPU seconds spent in user mode.
          %S     CPU seconds spent in kernel mode.
          %E     Elapsed time in seconds.
          %P     The CPU percentage, computed as 100*(%U+%S)/%E.
          %W     Number of times the process was swapped.
          %X     The average amount in (shared) text space used in kilobytes.
          %D     The average amount in (unshared) data/stack space used in kilobytes.
          %K     The total space used (%X+%D) in kilobytes.
          %M     The  maximum memory the process had in use at any time in kilobytes.
          %F     The number of major page faults (page needed to be brought from disk).
          %R     The number of minor page faults.
          %I     The number of input operations.
          %O     The number of output operations.
          %r     The number of socket messages received.
          %s     The number of socket messages sent.
          %k     The number of signals received.
          %w     Number of voluntary context switches (waits).
          %c     Number of involuntary context switches.
          %J     The name of this job.

          A  star may be inserted between the percent sign and flags printing time (e.g., `%*E'); this causes the time to be printed

in hh:mm:ss.ttt' format (hours and minutes are only printed if they are not zero). Alternatively, m' or u' may be used (e.g., %mE') to produce time output in milliseconds or microseconds, respectively.

From this you can learn several things:

The time measurements made by time covers both the time used by zsh itself (for interpreting your shell script) as well the time used by any commands spawned by the shell script to do the actual work.

The time measurements are split in a "user" and a "system" part. A program is considered consuming "user" time when it is executing on its own (i.e. doing calculations, moving data around in RAM, etc). It consumes "system" time when it has asked the operating system to do something on its behalf (for example reading data in from a file, sending data on a network, etc).

In addition to the above, you'll see that the last part of the output is the total time. This is understood as being a "wall-clock" time. I.e. the amount of time spent in "user" and "system" mode is determined as the amount of time the program is somehow consuming processor time, whereas the total time is the amount of time passed in the real world - including the time where the program is not actively doing anything and the system is also not doing anything on behalf of the program.

Finally the "99% cpu" part states the ratio between the combined user and system time consumption, and the total wall-clock time spent. For you the "99%" measurement means that your program was in some form actively using the CPU almost all the time. A low number would indicate that the system was busy with other things, and your program had periods of idle time.