Class: Monotonic::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/Monotonic/Timer.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.time(&block) ⇒ Object



9
10
11
12
# File 'lib/Monotonic/Timer.rb', line 9

def time(&block)
  timer = Timer.new
  timer.time(&block)
end

Instance Method Details

#startObject

class << self



15
16
17
18
# File 'lib/Monotonic/Timer.rb', line 15

def start
  @finish_time = nil
  @start_time = Monotonic::Time.now
end

#stopObject



20
21
22
# File 'lib/Monotonic/Timer.rb', line 20

def stop
  @finish_time = Monotonic::Time.now
end

#timeObject



32
33
34
35
36
37
38
39
40
# File 'lib/Monotonic/Timer.rb', line 32

def time
  start
  begin
    yield self
  ensure
    stop
  end
  total_time
end

#total_timeObject



24
25
26
27
28
29
30
# File 'lib/Monotonic/Timer.rb', line 24

def total_time
  if @finish_time
    @finish_time - @start_time
  else
    Monotonic::Time.now - @start_time
  end
end