/usr/bin/time --format output elapsed time in milliseconds

Preexo picture Preexo · Jun 6, 2013 · Viewed 23.3k times · Source

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 :)

Answer

devnull picture devnull · Jun 6, 2013

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.