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.



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/standard/facets/timer.rb', line 54

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.



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

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:



192
193
194
195
# File 'lib/standard/facets/timer.rb', line 192

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

Instance Method Details

#defuseObject

Kill time limit thread, if any.



126
127
128
129
130
131
132
# File 'lib/standard/facets/timer.rb', line 126

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

#end_timeObject



76
77
78
# File 'lib/standard/facets/timer.rb', line 76

def end_time
  @end_time
end

#limit(time_limit = nil) ⇒ Object

Establish a time limit on execution.



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/standard/facets/timer.rb', line 111

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



81
82
83
84
85
86
87
88
# File 'lib/standard/facets/timer.rb', line 81

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.



150
151
152
153
154
155
156
157
158
# File 'lib/standard/facets/timer.rb', line 150

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


165
166
167
168
169
170
# File 'lib/standard/facets/timer.rb', line 165

def reset_limit
  #stop
  #start
  defuse
  limit
end

#running?Boolean

Queries whether the timer is still running.

Returns:

  • (Boolean)


173
174
175
# File 'lib/standard/facets/timer.rb', line 173

def running?
  return @running
end

#startObject

Start the timer.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/standard/facets/timer.rb', line 91

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



71
72
73
# File 'lib/standard/facets/timer.rb', line 71

def start_time
  @start_time
end

#stopObject

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



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/standard/facets/timer.rb', line 136

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)


178
179
180
# File 'lib/standard/facets/timer.rb', line 178

def stopped?
  return !@running
end

#total_timeObject

Queries total recorded time of timer.



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

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