Class | Timer |
In: |
lib/more/facets/timer.rb
|
Parent: | Object |
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 TimeoutError, "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.
time_limit | [RW] |
Stops and resets the timer. If the timer was running returns the total time. If not returns 0.