Monday, March 14, 2022

[SOLVED] /usr/bin/time --format output elapsed time in milliseconds

Issue

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


Solution

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.



Answered By - devnull
Answer Checked By - Dawn Plyler (WPSolving Volunteer)