How to execute operation in milliseconds in Ruby?

I want to find out how many milliseconds a particular function is using. Therefore, I looked high and low, but could not find a way to get time in Ruby accurate to the millisecond.

How do you do this? In most programming languages, it's just something like

start = now.milliseconds myfunction() end = now.milliseconds time = end - start 
+62
ruby timer
Feb 18 2018-10-18
source share
9 answers

You can use the ruby ​​Time class. For example:

 t1 = Time.now # processing... t2 = Time.now delta = t2 - t1 # in seconds 

Now delta is a float object, and you can get as fine as a result how the class will be provided.

+75
Feb 18 2018-10-18
source share

You can also use the built-in Benchmark.measure function:

 require "benchmark" puts(Benchmark.measure { sleep 0.5 }) 

Print

 0.000000 0.000000 0.000000 ( 0.501134) 
+43
Sep 04 '12 at 17:08
source share

You should take a look at the reference module to run the tests. However, as a quick and dirty synchronization method, you can use something like this:

 def time now = Time.now.to_f yield endd = Time.now.to_f endd - now end 

Pay attention to the use of Time.now.to_f , which unlike to_i will not truncate to a few seconds.

+12
Feb 18 '10 at 14:50
source share

Use Time.now.to_f

+1
Feb 18 2018-10-18
source share

The absolute_time driver is a replacement replacement for Benchmark, but using native instructions is much more accurate.

+1
Apr 08 '13 at 4:08
source share

Using Time.now (which returns the wall clock time), because baselines have a couple of issues that can lead to unexpected behavior. This is due to the fact that the wall current time can be changed, for example, inserted jump-seconds or turn time , in order to set the local time to the time reference.

If there is, for example, the second second inserted during the measurement will be turned off for a second. Likewise, depending on local system conditions, you may have to deal with daytime, fast or slow running hours or hours, even skipping back in time, which leads to negative duration and many other problems.

The solution to this problem is to use a different time: a monotonous watch. This type of clock has different properties than a wall clock. It increases monotonously, i.e. It never returns and increases at a constant speed. However, it is not a wall clock (i.e., the time you read from the clock on the wall), but a time stamp that you can compare with a later time stamp to get the difference.

In Ruby, you can use such a timestamp with Process.clock_gettime(Process::CLOCK_MONOTONIC) as follows:

 t1 = Process.clock_gettime(Process::CLOCK_MONOTONIC) # => 63988.576809828 sleep 1.5 # do some work t2 = Process.clock_gettime(Process::CLOCK_MONOTONIC) # => 63990.08359163 delta = t2 - t1 # => 1.5067818019961123 delta_in_milliseconds = delta * 1000 # => 1506.7818019961123 

The Process.clock_gettime method returns a timestamp as a float with fractional seconds. The returned actual number does not have a specific value (which you must 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 difference in real time.

These attributes make the method the main candidate for measuring time differences, not seeing that your program failed at the least suitable time (for example, at midnight on New Year's Eve, when another insert is inserted).

+1
Aug 03 '17 at 11:41 on
source share

Using Time.now.to_i returns the second that has passed since 1970/01/01. Knowing this, you can do

 date1 = Time.now.to_f date2 = Time.now.to_f diff = date2 - date1 

In this case, you will have a difference in the second . If you want in milliseconds , just add to the code

 diff = diff * 1000 
0
Feb 21 '14 at 12:43
source share

If you use

 date = Time.now.to_i 

You get time in seconds, which is far from accurate, especially if you synchronize small code fragments.

0
Mar 10 '14 at 12:52
source share

I have a gem that can profile your ruby ​​method (instance or class) - https://github.com/igorkasyanchuk/benchmark_methods .

No more code:

 t = Time.now user.calculate_report puts Time.now - t 

Now you can do:

 benchmark :calculate_report # in class 

And just call your method

 user.calculate_report 
0
Oct 13 '15 at 6:26
source share



All Articles