Skip to content

Instantly share code, notes, and snippets.

@Frodox
Forked from linse/time.md
Last active August 29, 2015 14:13
Show Gist options
  • Save Frodox/e99030ebfa82da78df49 to your computer and use it in GitHub Desktop.
Save Frodox/e99030ebfa82da78df49 to your computer and use it in GitHub Desktop.

The Unix time command

man time is a good start - on Linux you get the GNU time version.

Note: some shells (e.g.,bash(1)) have a built-in time command that provides less functionality than the command described here. To access the real command, you may need to specify its pathname (something like /usr/bin/time).

GNU time

A call of /usr/bin/time gives a lot of information:

/usr/bin/time ls > /dev/null
0.00user 0.00system 0:00.00elapsed 0%CPU (0avgtext+0avgdata 3536maxresident)k
0inputs+0outputs (0major+265minor)pagefaults 0swaps

The information can be formatted with a format string (see man time), and we could just output the max. resident memory for example:

/usr/bin/time -f "mem: %M kilobytes" ls > /dev/null
mem: 3520 kilobytes

bash time

A call of time in bash, the bash builtin, gives us less information but more precise times:

time ls > /dev/null

real  0m0.003s
user  0m0.001s
sys 0m0.001s

It can be formatted like this:

TIMEFORMAT='%lU';time ( ls ) 2>&1 1>/dev/null

TIMEFORMAT opts:

TIMEFORMAT
              The value of this parameter is used as a format string specifying how the  timing  information  for  pipelines
              prefixed  with the time reserved word should be displayed.  The % character introduces an escape sequence that
              is expanded to a time value or other information.  The escape sequences and their meanings are as follows; the
              braces denote optional portions.
              %%        A literal %.
              %[p][l]R  The elapsed time in seconds.
              %[p][l]U  The number of CPU seconds spent in user mode.
              %[p][l]S  The number of CPU seconds spent in system mode.
              %P        The CPU percentage, computed as (%U + %S) / %R.

              The  optional p is a digit specifying the precision, the number of fractional digits after a decimal point.  A
              value of 0 causes no decimal point or fraction to be output.  At most three places after the decimal point may
              be specified; values of p greater than 3 are changed to 3.  If p is not specified, the value 3 is used.

              The optional l specifies a longer format, including minutes, of the form MMmSS.FFs.  The value of p determines
              whether or not the fraction is included.

              If this variable is not set, bash acts as if it had the value  $'\nreal\t%3lR\nuser\t%3lU\nsys%3lS'.   If  the
              value is null, no timing information is displayed.  A trailing newline is added when the format string is dis‐
              played.`

So, to print only seconds task elapsed:

(TIMEFORMAT="%R"; time cmd;)

Why is bash time more precise then GNU time?

The builtin bash command time gives milisecond precision of execution, and GNU time (usually /usr/bin/time) gives centisecond precision. The times(2) syscall gives times in clocks, and 100 clocks = 1 second (usually), so the precision is like GNU time. What is bash time using so that it is more precise?

Bash time internally uses getrusage() and GNU time uses times(). getrusage() is far more precise because of microsecond resolution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment