Measuring execution time of a command in milliseconds

22,567

The time command itself is not capable of doing this directly. It does output a time in a fractional format, so it can be parsed back into milliseconds by multiplying by 1000:

$ time sleep 1s
> real    0m1.006s
$ echo '1.006 * 1000' | bc
> 1006.000

This means you can kinda make a command that does it via:

{ time $@ ; } |& grep real | sed -E 's/[^0-9\.]+//g' | tr -d '\n' | (cat && echo " * 1000") | bc

That's essentially parsing the output of time and multiplying it by 1000. It's a mess because, well, bash.

Probably a better/more useful mess would be this script user Infineight posted on Stack Overflow:

#!/bin/bash
ts=$(date +%s%N)
$@
echo $((($(date +%s%N) - $ts)/1000000))

This (I have expanded it for readability) saves the current nanosecond timestamp from date, runs the command, then does a subtraction to calculate the time elapsed and formats it into milliseconds.

In my testing they both seemed to yield the same results.

Share:
22,567

Related videos on Youtube

user877529
Author by

user877529

Updated on September 18, 2022

Comments

  • user877529
    user877529 over 1 year

    I'm running Ubuntu 18.04 and am new to Ubuntu and Linux in general. I'm trying to measure the execution time of a command down to the millisecond. I would also like to append this time to a file, because I'm doing this a lot of times in a for loop. Finally, I would like to have the most simple and easily readable syntax.

    Long story short : I would like the /usr/bin/time command to return a result precise to the millisecond.

    I have read other threads mentioning a time format environment variable, but never how to modify it.

    Thanks in advance for the help.

    EDIT : Taking all answers into account, the solution was something like

    #!/bin/bash  
    ts=$(date +%s%N)  
    command  
    echo "formatting $((($(date +%s%N) - $ts)/1000000)) formatting" >> file_to_append_time_to  
    
    • guiverc
      guiverc over 5 years
      Use > to cause the output of a command to write to a file, >> to append to the file (rather than overwrite it). Have you even looked at the documentation? man time tells you "The elapsed time is not collected atomically with the execution of the program; as a result, in bizarre circumstances .." which means I suspect you want more from it that it can accurately provide (or should be trusted for). It also has examples, or try info time which contains more info & examples! Have you tried looking yourself?
  • Doug Smythies
    Doug Smythies over 5 years
    The time command you are using in the answer is the one built into bash. The question is specifically about the /usr/bin/time command, which is different. I upvoted anyhow, because I like your other solution.
  • Kristopher Ives
    Kristopher Ives over 5 years
    Thanks @DougSmythies, what is the difference between the two? I am curious. Thanks again.
  • Doug Smythies
    Doug Smythies over 5 years
    Perhaps this will help. I only remember because I have been caught be it so many times.
  • user877529
    user877529 over 5 years
    Thank you for your answer. I have already tried something that looks like the second solution, but I later found the formatting options of time to be very convenient for my purpose. What would be a syntax equivalent to time -f "%e" -o /path/to/file -a command but using your method to get ms, for example?
  • user877529
    user877529 over 5 years
    In fact, I would add that I have inverse needs compared to the author of the question in your link. Changing a TIMEFORMAT environment variable is the least of my concerns since I'm on a disposable VM for an assignment. I just don't know how to do it. I tried the set command but didn't find such variable in the list ...