5

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
0

2 Answers 2

3

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.

1
  • As an example of the difference between "user", "system", and "total", run: time sleep 10. The output is similar to sleep 10 0.00s user 0.00s system 0% cpu 10.008 total showing that the command took 10 seconds to run, but consumed no CPU time (it wasn't using 10 seconds of CPU time, just wall-clock time)
    – Josh
    Feb 12, 2021 at 13:45
1

From man bash

If the time reserved word precedes a pipeline, the elapsed as  well  as
user  and  system  time consumed by its execution are reported when the
pipeline terminates.

So you see the time spent in user space, the time spent in the system/kernel, and the overall time the command took.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .