Class: Timer

Inherits:
Object show all
Defined in:
lib/standard/facets/timer.rb

Overview

Timer

Provides a strightforward means for controlling time critical execution. Can be used as a “stop watch” timer or as a “time bomb” timer:

t = Timer.new(10) { raise Timeout::Error, "timeout!" }
t.start
  :      # done within 10sec timeout
t.stop
t.start
  :
if condition then
  t.reset       #--> restart timer
end

A class method is also provided for easily timing the exectuion of a block.

Timer.time do |timer|
 timer.total_time.round  #=> 0

 sleep 1
 timer.total_time.round  #=> 1

 timer.stop
 timer.total_time.round  #=> 1

 sleep 1
 timer.total_time.round  #=> 1

 timer.start
 timer.total_time.round  #=> 1

 sleep 1
 timer.total_time.round  #=> 2
end

Thanks to Paul Brannan for TimeLimit and Minero Aoki for Timer. These two libraries served as models for building this class.

Direct Known Subclasses

Dummy

Defined Under Namespace

Classes: Dummy

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(time_limit = nil, &block) ⇒ Timer

Returns a new instance of Timer.



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/standard/facets/timer.rb', line 45

def initialize(time_limit=nil, &block)
  # standard timer
  @start_time = nil
  @end_time = nil
  @total_time = 0
  @runnning = nil
  # for using time limit
  @time_limit = time_limit
  @on_timeout = block
  @current_thread = nil
  @timer_thread = nil
end

Instance Attribute Details

#time_limitObject

Returns the value of attribute time_limit.



59
60
61
# File 'lib/standard/facets/timer.rb', line 59

def time_limit
  @time_limit
end

Class Method Details

.time {|timer = Timer.new.start| ... } ⇒ Object

Takes a block and returns the total time it took to execute.

Yields:



183
184
185
186
# File 'lib/standard/facets/timer.rb', line 183

def self.time
  yield( timer = Timer.new.start )
  return timer.total_time
end

Instance Method Details

#defuseObject

Kill time limit thread, if any.



117
118
119
120
121
122
123
# File 'lib/standard/facets/timer.rb', line 117

def defuse
  if @timer_thread
    #Thread.kill @timer_thread
    @timer_thread.kill
    @timer_thread = nil
  end
end

#end_timeObject



67
68
69
# File 'lib/standard/facets/timer.rb', line 67

def end_time
  @end_time
end

#limit(time_limit = nil) ⇒ Object

Establish a time limit on execution.



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/standard/facets/timer.rb', line 102

def limit( time_limit=nil )
  if @time_limit || time_limit
    @current_thread = Thread.current
    @timer_thread = Thread.fork {
      sleep @time_limit
      if @on_timeout then
        @on_timeout.call @time_limit
      else
        @current_thread.raise Timeout::Error, "#{@time_limit} seconds past"
      end
    }
  end
end

#on_timeout(&block) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/standard/facets/timer.rb', line 72

def on_timeout( &block )
  if block then
    @on_timeout = block
    true
  else
    false
  end
end

#resetObject

Stops and resets the timer. If the timer was running returns the total time. If not returns 0.



141
142
143
144
145
146
147
148
149
# File 'lib/standard/facets/timer.rb', line 141

def reset
  if running?
    r = stop
  else
    r = 0
  end
  @total_time = 0
  return r
end

#reset_limitObject

Resets the time limit. Same as:

t.stop
t.start


156
157
158
159
160
161
# File 'lib/standard/facets/timer.rb', line 156

def reset_limit
  #stop
  #start
  defuse
  limit
end

#running?Boolean

Queries whether the timer is still running.

Returns:

  • (Boolean)


164
165
166
# File 'lib/standard/facets/timer.rb', line 164

def running?
  return @running
end

#startObject

Start the timer.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/standard/facets/timer.rb', line 82

def start
  @running = true
  @start_time = Time.now

  limit if @time_limit

  self

  ##if block_given? then
  ##  begin
  ##    yield( self )
  ##  ensure
  ##    stop
  ##  end
  ##else
  ##  @time_limit
  ##end
end

#start_timeObject



62
63
64
# File 'lib/standard/facets/timer.rb', line 62

def start_time
  @start_time
end

#stopObject

Stops timer and returns total time. If timer was not running returns false.



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/standard/facets/timer.rb', line 127

def stop
  if @running
    defuse
    # record running time
    @end_time = Time.now
    @running = false
    @total_time += (@end_time - @start_time)
  else
    nil
  end
end

#stopped?Boolean

Queries whether the timer is still not running.

Returns:

  • (Boolean)


169
170
171
# File 'lib/standard/facets/timer.rb', line 169

def stopped?
  return !@running
end

#total_timeObject

Queries total recorded time of timer.



174
175
176
177
178
179
180
# File 'lib/standard/facets/timer.rb', line 174

def total_time
  if running? then
    return @total_time + (Time.now - @start_time)
  else
    return @total_time
  end
end