Class: Async::Clock
- Inherits:
-
Object
- Object
- Async::Clock
- Defined in:
- lib/async/clock.rb
Overview
A convenient wrapper around the internal monotonic clock.
Class Method Summary collapse
-
.measure ⇒ Object
Measure the execution of a block of code.
-
.now ⇒ Object
Get the current elapsed monotonic time.
-
.start ⇒ Object
Start measuring elapsed time from now.
Instance Method Summary collapse
-
#initialize(total = 0) ⇒ Clock
constructor
Create a new clock with the initial total time.
-
#reset! ⇒ Object
Reset the total elapsed time.
-
#start! ⇒ Object
Start measuring a duration.
-
#stop! ⇒ Object
Stop measuring a duration and append the duration to the current total.
-
#total ⇒ Object
The total elapsed time including any current duration.
Constructor Details
permalink #initialize(total = 0) ⇒ Clock
Create a new clock with the initial total time.
34 35 36 37 |
# File 'lib/async/clock.rb', line 34 def initialize(total = 0) @total = total @started = nil end |
Class Method Details
permalink .measure ⇒ Object
Measure the execution of a block of code.
18 19 20 21 22 23 24 |
# File 'lib/async/clock.rb', line 18 def self.measure start_time = self.now yield return self.now - start_time end |
permalink .now ⇒ Object
Get the current elapsed monotonic time.
11 12 13 |
# File 'lib/async/clock.rb', line 11 def self.now ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) end |
permalink .start ⇒ Object
Start measuring elapsed time from now.
28 29 30 |
# File 'lib/async/clock.rb', line 28 def self.start self.new.tap(&:start!) end |
Instance Method Details
permalink #reset! ⇒ Object
Reset the total elapsed time. If the clock is currently running, reset the start time to now.
66 67 68 69 70 71 72 |
# File 'lib/async/clock.rb', line 66 def reset! @total = 0 if @started @started = Clock.now end end |
permalink #start! ⇒ Object
Start measuring a duration.
40 41 42 |
# File 'lib/async/clock.rb', line 40 def start! @started ||= Clock.now end |
permalink #stop! ⇒ Object
Stop measuring a duration and append the duration to the current total.
45 46 47 48 49 50 51 52 |
# File 'lib/async/clock.rb', line 45 def stop! if @started @total += (Clock.now - @started) @started = nil end return @total end |
permalink #total ⇒ Object
The total elapsed time including any current duration.
55 56 57 58 59 60 61 62 63 |
# File 'lib/async/clock.rb', line 55 def total total = @total if @started total += (Clock.now - @started) end return total end |