class Hitimes::Interval

This is the lowest level timing mechanism available. It allows for easy measuring based upon a block:

duration = Interval.measure { ... }

Or measuring something specifically

interval = Interval.new
interval.start
duration = interval.stop

Allocating and starting an interval can be done in one method call with

interval = Interval.now

Interval is useful when you only need to track a single interval of time, or if you do not want to track statistics about an operation.

Public Class Methods

measure { } → Float click to toggle source

Times the execution of the block returning the number of seconds it took

# File lib/hitimes/interval.rb, line 51
def self.measure(&block)
  raise Error, "No block given to Interval.measure" unless block_given?

  i = Interval.now
  yield
  i.stop

  return i.duration
end
new(start = nil, stop = nil) click to toggle source
# File lib/hitimes/interval.rb, line 32
def initialize(start = nil, stop = nil)
  @start_instant = start
  @stop_instant  = stop
  @duration      = -Float::INFINITY
end
now → Interval click to toggle source

Create an interval that has already started

# File lib/hitimes/interval.rb, line 43
def self.now
  Interval.new(Hitimes.raw_instant)
end

Public Instance Methods

duration → Float click to toggle source

Returns

Returns the Float value of the interval, the value is in seconds. If the interval has not had stop called yet, it will report the number of seconds in the interval up to the current point in time.

Raises Error if duration is called on an interval that has not started yet.

# File lib/hitimes/interval.rb, line 149
def duration
  raise Error, "Attempt to report a duration on an interval that has not started" unless started?

  return duration_so_far unless stopped?

  if @duration < 0 then
    @duration = calculate_duration(@start_instant, @stop_instant)
  end

  return @duration
end
Also aliased as: to_f, to_seconds, length
duration_so_far → Float or false click to toggle source

return how the duration so far. This will return the duration from the time the Interval was started if the interval is running, otherwise it will return false.

# File lib/hitimes/interval.rb, line 104
def duration_so_far
  return false unless running?

  _now = Hitimes.raw_instant
  calculate_duration(@start_instant, _now)
end
length → Float
Alias for: duration
running? → boolean click to toggle source

returns whether or not the interval is running or not. This means that it has started, but not stopped.

# File lib/hitimes/interval.rb, line 133
def running?
  started? && !stopped?
end
split → Interval click to toggle source

Immediately stop the current interval and start a new interval that has a start_instant equivalent to the stop_interval of self.

# File lib/hitimes/interval.rb, line 66
def split
  @stop_instant = ::Hitimes.raw_instant
  return Interval.new(@stop_instant)
end
start → boolean click to toggle source

mark the start of the interval. Calling start on an already started interval has no effect. An interval can only be started once. If the interval is truely started true is returned otherwise false.

# File lib/hitimes/interval.rb, line 77
def start
  return false if started?
  @start_instant = ::Hitimes.raw_instant
  true
end
started? → boolean click to toggle source

returns whether or not the interval has been started

# File lib/hitimes/interval.rb, line 115
def started?
  !!@start_instant
end
stop → bool or Float click to toggle source

mark the stop of the interval. Calling stop on an already stopped interval has no effect. An interval can only be stopped once. If the interval is truely stopped then the duration is returned, otherwise false.

# File lib/hitimes/interval.rb, line 89
def stop
  raise Error, "Attempt to stop an interval that has not started" unless started?
  return false if stopped?

  @stop_instant = ::Hitimes.raw_instant

  return duration
end
stopped? → boolean click to toggle source

returns whether or not the interval has been stopped

# File lib/hitimes/interval.rb, line 123
def  stopped?
  !!@stop_instant
end
to_f → Float
Alias for: duration
to_seconds → Float
Alias for: duration

Private Instance Methods

calculate_duration(start, stop) click to toggle source
# File lib/hitimes/interval.rb, line 167
def calculate_duration(start, stop)
  (stop - start) / ::Hitimes::INSTANT_CONVERSION_FACTOR
end

Public

↑ top

Attributes

start_instant[R]

The integer representing the start instant of the Interval. This valuea is not useful on its own. It is a platform dependent value.

stop_instant[R]

The integer representing the stop instant of the Interval. This value is not useful on its own. It is a platform dependent value.