40

I use the /usr/bin/time program to measure the time for a command. with the --format parameter i can format the output. e.g.

/usr/bin/time -f "%e" ls

is there a way to output a bigger accuracy of the elapsed seconds? or just output milliseconds, not seconds?

In the manual of /usr/bin/time it only says something about seconds, but maybe there is a way and someone can help me... thanks!

EDIT: I know about the bash command "time" which uses the format of the environment variable "TIMEFORMAT". sorry, but i don't wanna change that env-var... seems to risky to me, solution should be something that doesn't change the running system at all :)

Preexo
  • 2,102
  • 5
  • 29
  • 37
  • The man page says nothing about that. So I assume that it is not possible using an *unchanged* version of time. It should be possible to craft a patch for the output format, but I don't know whether it is possible to get a higher accuracy. – hek2mgl Jun 06 '13 at 10:47
  • Just pointing out for future readers that the `TIMEFORMAT` variable is _only_ used for controlling the output of the builtin `time` command. There's no risk associated with changing it. – David Z Oct 15 '14 at 01:57
  • Also changes to a shell only affect that shell. – studog Jun 04 '18 at 20:55

3 Answers3

51

One possibility is to use the date command:

ts=$(date +%s%N) ; my_command ; tt=$((($(date +%s%N) - $ts)/1000000)) ; echo "Time taken: $tt milliseconds"

%N should return nanoseconds, and 1 millisecond is 1000000 nanosecond, hence by division would return the time taken to execute my_command in milliseconds.

NOTE that the %N is not supported on all systems, but most of them.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
devnull
  • 118,548
  • 33
  • 236
  • 227
  • 1
    excelent, accepted! didn't think of date, but that's good enough! To get from nanoseconds to milliseconds you have to divide by 1000000, though: ts=$(date +%s%N) ; sleep 1 ; tt=$((($(date +%s%N) - $ts)/1000000)) ; echo $tt – Preexo Jun 06 '13 at 13:12
  • 3
    For those on OSX without the required temporal resolution from date, one can substitute the native date with gdate. For example: `brew install coreutils` if necessary and then `ts=$(gdate +%s%N) ; sleep 1 ; tt=$((($(gdate +%s%N) - $ts)/1000000)) ; echo $tt` [source](http://apple.stackexchange.com/questions/135742/time-in-milliseconds-since-epoch-in-the-terminal) – snodnipper Oct 06 '16 at 08:49
  • You can use %3N on systems that have %N to get just the milliseconds. See `https://serverfault.com/a/588705/432437` – studog Jun 04 '18 at 21:32
10

For convenience I made devnull's answer into a script (I named it millisecond-time).

#!/bin/bash
ts=$(date +%s%N) ; $@ ; tt=$((($(date +%s%N) - $ts)/1000000)) ; echo "Time taken: $tt milliseconds"

I put the script in /usr/local/bin.
Gave it execute rights chmod +x /usr/local/bin/millisecond-time.
Now I can use it like this: millisecond-time my_command

P.s. This would be a comment if I had the rep'.

Infineight
  • 498
  • 5
  • 10
2

There are a couple of things getting confused in this thread.

Bash has a built-in time command which supports a TIMEFORMAT environment variable that will let you format the output. For details on this run man bash and search for TIMEFORMAT.

There is also a standard /usr/bin/time command-line utility which supports a TIME environment variable that will let you format the output (or you can use -f or --format on the command line). For details on this run man time and search for TIME.

If you want the number of seconds the command took to run you can either use the built-in bash command (which supports a maximum precision of three decimal places):

bash# export TIMEFORMAT="%3lR" 
bash# time find /etc > /dev/null
0m0.015s

Or you can use the command-line utility (which supports a maximum precision of two decimal places):

shell# export TIME="%E"
shell# /usr/bin/time find /opt/ > /dev/null
0:00.72

As mentioned above neither of these variables are used by anything else and are safe to change.

Adam Shand
  • 193
  • 9