timer.rb

Path: lib/more/facets/timer.rb
Last Update: Thu Jun 24 13:18:25 +0000 2010

Timer

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

Usage

  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 Kernel method is also provided for easily timing the exectuion of a block.

  timed { |timer|

     timer.total_time.round #=> 0

     sleep 1
     timer.total_time.round #=> 1

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

     sleep 2
     timer.total_time.round #=> 1

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

     sleep 1
     timer.total_time.round #=> 2

  }

HISTORY

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

AUTHORS

  • Thomas Sawyer
  • Minero Aoki
  • Paul Brannan

COPYRIGHT

  Copyright (c) 2004 Thomas Sawyer

LICENSE

  Ruby License

  This module is free software. You may use, modify, and/or redistribute this
  software under the same terms as Ruby.

  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  FOR A PARTICULAR PURPOSE.

Required files

timeout  

[Validate]