Class: Timer
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
Defined Under Namespace
Classes: Dummy
Instance Attribute Summary collapse
-
#time_limit ⇒ Object
Returns the value of attribute time_limit.
Class Method Summary collapse
-
.time {|timer = Timer.new.start| ... } ⇒ Object
Takes a block and returns the total time it took to execute.
Instance Method Summary collapse
-
#defuse ⇒ Object
Kill time limit thread, if any.
- #end_time ⇒ Object
-
#initialize(time_limit = nil, &block) ⇒ Timer
constructor
A new instance of Timer.
-
#limit(time_limit = nil) ⇒ Object
Establish a time limit on execution.
- #on_timeout(&block) ⇒ Object
-
#reset ⇒ Object
Stops and resets the timer.
-
#reset_limit ⇒ Object
Resets the time limit.
-
#running? ⇒ Boolean
Queries whether the timer is still running.
-
#start ⇒ Object
Start the timer.
- #start_time ⇒ Object
-
#stop ⇒ Object
Stops timer and returns total time.
-
#stopped? ⇒ Boolean
Queries whether the timer is still not running.
-
#total_time ⇒ Object
Queries total recorded time of timer.
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_limit ⇒ Object
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
Instance Method Details
#defuse ⇒ Object
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_time ⇒ Object
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 |
#reset ⇒ Object
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_limit ⇒ Object
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.
164 165 166 |
# File 'lib/standard/facets/timer.rb', line 164 def running? return @running end |
#start ⇒ Object
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_time ⇒ Object
62 63 64 |
# File 'lib/standard/facets/timer.rb', line 62 def start_time @start_time end |
#stop ⇒ Object
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.
169 170 171 |
# File 'lib/standard/facets/timer.rb', line 169 def stopped? return !@running end |