Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I've checked this , but the solution provided did not work for me.

Using date I can get the execution time in seconds:

T="$(date +%s)"
#some work here
T="$(($(date +%s)-T))"
echo "execution time (secs) ${T}"

However, when I try this:

T="$(date +%s%N)"
#some work here
T="$(($(date +%s%N)-T))"

Adding %N (as above) to the date to get the nano-second precision I get the following error: -bash: 1369320760N: value too great for base (error token is "1369320760N")

Does anyone know how to measure the execution time in milliseconds using bash in mac osx ?

share|improve this question
    
Do you really need nano second resolution? Which this approach you have a lot of overhead. With # some work here ommited, I get times from 0.2 to 0.3 sec -- what should be zero (or at least constant) to reach your goal. Whereas /usr/bin/time usleep 50000 gives a reasonale stable time of 0.05 +/- 0.01 sec. (Only tested in Linux, don't know if /usr/bin/time and/or usleep is available on OSX.) –  mpy May 23 '13 at 15:18
    
Are you sure the internal clock resolution offers ACCURATE resolution down to the nano-second level? –  mdpc May 23 '13 at 17:14
add comment

5 Answers 5

Since OSX is a BSD-ish system, it's strftime library doesn't have %N (http://www.freebsd.org/cgi/man.cgi?query=date)

Does the bash builtin time command give you information you need?

time some work here
share|improve this answer
    
thanks for making it clear, time works perfectly and outputs three results, but I need to specifically work with the initial and final times. –  pacodelumberg May 23 '13 at 15:16
add comment

echo $(($(date +%s%N)/1000000))

This would convert from nano-seconds to milli-seconds.

From @mpy, date +%s.%N works.

Output is different between linux and OS X though:
linux: 1369322592.573787111
OS X: 1369322610.N

share|improve this answer
    
I am getting the following error: -bash: 1369321446N: value too great for base (error token is "1369321446N")this does not work in mac osx, I am getting: –  pacodelumberg May 23 '13 at 15:04
    
Well, that's annoying... (tested on wrong window) seeing same error :/ –  demure May 23 '13 at 15:11
1  
No need to divide the output, just use date +%s.%N (but I can't check on OSX; it's working on Linux). But the problem then is, that bash's $(( )) IMHO isn't able to handle floating point numbers. –  mpy May 23 '13 at 15:14
    
@mpy That works on osx. Same out for the first 10 chars, acts differently between linux and OS X though. –  demure May 23 '13 at 15:17
    
Does that mean we cannot get the nano second information using date? –  pacodelumberg May 23 '13 at 15:57
show 1 more comment

As the other answer's mentioned, OS X's date doesn't support %N (nanoseconds).

Could you just save the commands to time as a script? Or use a subshell or command group for them:

time (sleep 0.1; echo a; sleep 0.1)
time { sleep 0.1; echo a; sleep 0.1; }

Or sum the times of individual commands:

$ time=$(TIMEFORMAT=%R; (time sleep 0.1; echo a) 2>&1 > /dev/null)
$ time+=+$(TIMEFORMAT=%R; (time sleep 0.1) 2>&1)
$ bc <<< $time
.206

Or use some scripting language:

$ ruby -e 'puts "%.3f" % Time.now'
1369329611.046
$ time ruby -e '""'
0.029
$ time ruby -e 'puts "%.3f" % Time.now'
1369329622.943
0.029

%.3f is a format specifier for a floating point number with 3 decimal points. Time.now is a class method of the Time class.

share|improve this answer
    
Thanks for your answer, could you please describe the ruby code above? –  pacodelumberg May 24 '13 at 7:22
add comment

Glenn is right, time is the command you are looking for, but in order to parse out the info you want, you will need to pass it along to some other processor like sed.

time command-goes-here >/dev/null 2>$1|sed rules-go-here

This command will get rid of the output from the command itself (sending both standard and error outputs to /dev/null), and pass the time results to sed, where you will need to choose the appropriate rules to edit the stream, and output the desired value.

share|improve this answer
add comment

I would go this route if I needed milliseconds and BSD didn't support date +%s%N

perl -MTime::HiRes -e 'printf("%.0f\n",Time::HiRes::time()*1000)'
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.