### Home Classes Methods

### In Files

- lib/facets/timer.rb

### Parent

Object

### Namespace

- CLASS Timer::Dummy

### Methods

- ::new
- ::time
- #defuse
- #end_time
- #limit
- #on_timeout
- #reset
- #reset_limit
- #running?
- #start
- #start_time
- #stop
- #stopped?
- #total_time

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- Random
- Random::ArrayExtensions
- Random::HashExtensions
- Random::IntegerExtensions
- Random::NumericExtensions
- Random::RangeExtensions
- Random::StringExtensions
- Random::StringExtensions::Self
- FileUtils
- FileUtils::DryRun
- FileUtils::NoWrite
- FileUtils::Verbose
- Digest
- Digest::Class
- Digest::Instance
- Multiton
- Multiton::Inclusive
- Multiton::MetaMethods
- URI
- URI::Hash
- URI::Kernel
- Argvector
- Argvector
- ERB
- ERB::OpenTemplate
- Enumerable
- Enumerable::Arguments
- GetoptLong
- GetoptLong::DSL
- Memoizable
- Memoizable::Copy
- Numeric
- Numeric::Multipliers
- Object
- Object::ArgVector
- Timer
- Timer::Dummy
- Array
- BasicObject
- CGI
- Cloneable
- Continuation
- Date
- DateTime
- Equitable
- File
- Find
- Hash
- Instantiable
- Integer
- Interval
- Kernel
- Math
- Module
- Net
- NilClass
- NullClass
- OpEsc
- OpenStruct
- Pathname
- Platform
- Range
- RbConfig
- Set
- Shellwords
- String
- StringScanner
- Time
- Tuple
- YAML
- Zlib

No matching classes.

# 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 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.

### Attributes

time_limit[RW]

### Public Class Methods

new(time_limit=nil, &block) click to toggle source

    # File lib/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

time() click to toggle source

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

    # File lib/facets/timer.rb, line 183
    def self.time
      yield( timer = Timer.new.start )
      return timer.total_time
    end

### Public Instance Methods

defuse() click to toggle source

Kill time limit thread, if any.

    # File lib/facets/timer.rb, line 117
    def defuse
      if @timer_thread
        #Thread.kill @timer_thread
        @timer_thread.kill
        @timer_thread = nil
      end
    end

end_time() click to toggle source

    # File lib/facets/timer.rb, line 67
    def end_time
      @end_time
    end

limit( time_limit=nil ) click to toggle source

Establish a time limit on execution.

    # File lib/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 TimeoutError, "#{@time_limit} seconds past"
          end
        }
      end
    end

on_timeout( &block ) click to toggle source

    # File lib/facets/timer.rb, line 72
    def on_timeout( &block )
      if block then
        @on_timeout = block
        true
      else
        false
      end
    end

reset() click to toggle source

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

    # File lib/facets/timer.rb, line 141
    def reset
      if running?
        r = stop
      else
        r = 0
      end
      @total_time = 0
      return r
    end

reset_limit() click to toggle source

Resets the time limit. Same as:

    t.stop
    t.start

    # File lib/facets/timer.rb, line 156
    def reset_limit
      #stop
      #start
      defuse
      limit
    end

running?() click to toggle source

Queries whether the timer is still running.

    # File lib/facets/timer.rb, line 164
    def running?
      return @running
    end

start() click to toggle source

Start the timer.

    # File lib/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() click to toggle source

    # File lib/facets/timer.rb, line 62
    def start_time
      @start_time
    end

stop() click to toggle source

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

    # File lib/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?() click to toggle source

Queries whether the timer is still not running.

    # File lib/facets/timer.rb, line 169
    def stopped?
      return !@running
    end

total_time() click to toggle source

Queries total recorded time of timer.

    # File lib/facets/timer.rb, line 174
    def total_time
      if running? then
        return @total_time + (Time.now - @start_time)
      else
        return @total_time
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.