15

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  
user877529
  • 151
  • 1
  • 1
  • 5
  • 1
    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? – guiverc Oct 01 '18 at 21:36

1 Answers1

11

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.

Kristopher Ives
  • 5,419
  • 2
  • 27
  • 36
  • 1
    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. – Doug Smythies Oct 02 '18 at 00:22
  • Thanks @DougSmythies, what is the difference between the two? I am curious. Thanks again. – Kristopher Ives Oct 02 '18 at 00:46
  • Perhaps [this](https://askubuntu.com/questions/434289/why-doesnt-the-time-command-work-with-any-option) will help. I only remember because I have been caught be it so many times. – Doug Smythies Oct 02 '18 at 00:57
  • 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 Oct 02 '18 at 07:52
  • 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 ... – user877529 Oct 02 '18 at 08:07