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)
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).
Holger Just Aug 03 '17 at 11:41 on 2017-08-03 11:41
source share