I'm wishing to figure out how many milliseconds a particular function uses. So I looked high and low, but could not find a way to get the time in Ruby with millisecond precision.
How do you do this? In most programming languages its just something like
start = now.milliseconds
myfunction()
end = now.milliseconds
time = end - start
I've a gem which can profile your ruby method (instance or class) - https://github.com/igorkasyanchuk/benchmark_methods.
No more code like this:
Now you can do:
And just call your method
You should take a look at the benchmark module to perform benchmarks. However, as a quick and dirty timing method you can use something like this:
Note the use of
Time.now.to_f
, which unlike to_i, won't truncate to seconds.The use of
Time.now.to_i
return the second passed from 1970/01/01. Knowing this you can doWith this you will have difference in second magnitude. If you want it in milliseconds, just add to the code
Using
Time.now
(which returns the wall-clock time) as base-lines has a couple of issues which can result in unexpected behavior. This is caused by the fact that the wallclock time is subject to changes like inserted leap-seconds or time slewing to adjust the local time to a reference time.If there is e.g. a leap second inserted during measurement, it will be off by a second. Similarly, depending on local system conditions, you might have to deal with daylight-saving-times, quicker or slower running clocks, or the clock even jumping back in time, resulting in a negative duration, and many other issues.
A solution to this issue is to use a different time of clock: a monotonic clock. This type of clock has different properties than the wall clock. It increments monitonically, i.e. never goes back and increases at a constant rate. With that, it does not represent the wall-clock (i.e. the time you read from a clock on your wall) but a timestamp you can compare with a later timestamp to get a difference.
In Ruby, you can use such a timestamp with
Process.clock_gettime(Process::CLOCK_MONOTONIC)
like follows:The
Process.clock_gettime
method returns a timestamp as a float with fractional seconds. The actual number returned has no defined meaning (that you should rely on). However, you can be sure that the next call will return a larger number and by comparing the values, you can get the real time difference.These attributes make the method a prime candidate for measuring time differences without seeing your program fail in the least opportune times (e.g. at midnight at New Year's Eve when there is another leap-second inserted).
The
Process::CLOCK_MONOTONIC
constant used here is available on all modern Linux, BSD, and macOS systems as well as the Linux Subsystem for Windows. It is however not yet available for "raw" Windows systems. There, you can use theGetTickCount64
system call instead ofProcess.clock_gettime
which also returns a timer value in millisecond granularity on Windows (>= Windows Vista, Windows Server 2008).With Ruby, you can call this function like this: