Skip to content

Instantly share code, notes, and snippets.

@Javieratapiab
Created March 29, 2020 04:52
Show Gist options
  • Select an option

  • Save Javieratapiab/4813002c2be8f704262d14fa6e9e6d1f to your computer and use it in GitHub Desktop.

Select an option

Save Javieratapiab/4813002c2be8f704262d14fa6e9e6d1f to your computer and use it in GitHub Desktop.

Revisions

  1. Javieratapiab created this gist Mar 29, 2020.
    53 changes: 53 additions & 0 deletions elapsed-time-ruby.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    ### THE WRONG WAY ###
    starting = Time.now
    # time consuming operation
    ending = Time.now
    elapsed = ending - starting
    elapsed # => 10.822178
    #####################

    ### THE GOOD WAY ###
    starting = Process.clock_gettime(Process::CLOCK_MONOTONIC)
    # time consuming operation
    ending = Process.clock_gettime(Process::CLOCK_MONOTONIC)
    elapsed = ending - starting
    elapsed # => 9.183449000120163 seconds

    =begin
    Depending on the low level Operating System (OS) settings,
    Ruby's Time.now uses `gettimeofday` or `clock_gettime` Linux functions from time.h.
    According to the documentation of gettimeofday:
    [It] gives the number of seconds and microseconds since the Epoch.
    It returns a struct with the number of seconds and the system time zone.
    Ruby VM can then calculate and it returns a Time object with these informations.
    This is often indicated as wall time in Linux documentation.
    However:
    The time returned by `gettimeofday()` is affected by discontinuous jumps in the system time
    (e.g., if the system administrator manually changes the system time).
    The lack of perfect accuracy is due to CPU physical conditions like temperature,
    air pressure, and even magnetic fields.
    So when the OS tries to set a new system time, it doesn't guarantee that the new value will be in the future.
    If a CPU has a clock that is "too fast", the OS can decide to reset the time of a few seconds backward.
    Another reason why wall clocks are flawed is because some CPUs can't manage leap seconds.
    On Wikipedia there is a page dedicated to incidents in software history due to leap seconds.
    Another reason why wall clocks are flawed is because some CPUs can't manage leap seconds.
    On Wikipedia there is a page dedicated to incidents in software history due to leap seconds.
    To recap: system clock is constantly floating and it doesn't move only forwards.
    If your calculation of elapsed time is based on it, you're very likely to run into calculation errors or even outages.
    Posix systems have solved this problem by introducing a monotonic clock.
    It's conceptually similar to a timer that starts with an event and it isn't affected by time floating problems.
    Each time you request the time to the monotonic clock, it returns the time since that event.
    On Mac OS, this event is the system boot.
    Alongside with monotonic, there are several clock types: realtime, monotonic raw, virtual just to name a few.
    Each of them solves a different problem.
    =end