Skip to content

Instantly share code, notes, and snippets.

@linse
Created September 17, 2012 15:02
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save linse/3737870 to your computer and use it in GitHub Desktop.
Save linse/3737870 to your computer and use it in GitHub Desktop.
Some facts about time

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

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.

@major-lab
Copy link

/usr/bin/time seems to report too much memory for max. resident size:
http://lkml.indiana.edu/hypermail/linux/kernel/1210.0/03883.html

@major-lab
Copy link

Other alternative: Use torque PBS or Sun/Oracle Grid Engine.

@tedtoal
Copy link

tedtoal commented Apr 25, 2016

gtime on OSX El Capitan 10.11.4 always reports 0 values for %t and %k. For %M it reports values that far exceed the amount of RAM in the system, such as 402685952kb on a 16 GB system.

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