Class: Async::Clock
- Inherits:
-
Object
- Object
- Async::Clock
- Defined in:
- lib/async/clock.rb
Class Method Summary collapse
-
.measure ⇒ Object
Measure the execution of a block of code.
-
.now ⇒ Object
Get the current elapsed monotonic time.
- .start ⇒ Object
Instance Method Summary collapse
-
#initialize(total = 0) ⇒ Clock
constructor
A new instance of Clock.
- #start! ⇒ Object
- #stop! ⇒ Object
- #total ⇒ Object
Constructor Details
#initialize(total = 0) ⇒ Clock
Returns a new instance of Clock.
43 44 45 46 |
# File 'lib/async/clock.rb', line 43 def initialize(total = 0) @total = total @started = nil end |
Class Method Details
.measure ⇒ Object
Measure the execution of a block of code.
31 32 33 34 35 36 37 |
# File 'lib/async/clock.rb', line 31 def self.measure start_time = self.now yield return self.now - start_time end |
.now ⇒ Object
Get the current elapsed monotonic time.
26 27 28 |
# File 'lib/async/clock.rb', line 26 def self.now ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) end |
.start ⇒ Object
39 40 41 |
# File 'lib/async/clock.rb', line 39 def self.start self.new.tap(&:start!) end |
Instance Method Details
#start! ⇒ Object
48 49 50 |
# File 'lib/async/clock.rb', line 48 def start! @started ||= Clock.now end |
#stop! ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/async/clock.rb', line 52 def stop! if @started @total += (Clock.now - @started) @started = nil end return @total end |
#total ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/async/clock.rb', line 61 def total total = @total if @started total += (Clock.now - @started) end return total end |