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
Source
# File lib/hitimes/interval.rb, line 52 def self.measure raise Error, "No block given to Interval.measure" unless block_given? interval = Interval.now yield interval.stop interval.duration end
Times the execution of the block returning the number of seconds it took
Source
# File lib/hitimes/interval.rb, line 33 def initialize(start = nil, stop = nil) @start_instant = start @stop_instant = stop @duration = -Float::INFINITY end
Source
# File lib/hitimes/interval.rb, line 44 def self.now Interval.new(Hitimes.raw_instant) end
Create an interval that has already started
Public Instance Methods
Source
# File lib/hitimes/interval.rb, line 151 def duration raise Error, "Attempt to report a duration on an interval that has not started" unless started? return duration_so_far unless stopped? @duration = calculate_duration(@start_instant, @stop_instant) if @duration.negative? @duration end
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.
Source
# File lib/hitimes/interval.rb, line 106 def duration_so_far return false unless running? raw = Hitimes.raw_instant calculate_duration(@start_instant, raw) end
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.
Source
# File lib/hitimes/interval.rb, line 135 def running? started? && !stopped? end
returns whether or not the interval is running or not. This means that it has started, but not stopped.
Source
# File lib/hitimes/interval.rb, line 67 def split @stop_instant = ::Hitimes.raw_instant Interval.new(@stop_instant) end
Immediately stop the current interval and start a new interval that has a start_instant
equivalent to the stop_interval of self.
Source
# File lib/hitimes/interval.rb, line 78 def start return false if started? @start_instant = ::Hitimes.raw_instant true end
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
.
Source
# File lib/hitimes/interval.rb, line 117 def started? !!@start_instant end
returns whether or not the interval has been started
Source
# File lib/hitimes/interval.rb, line 91 def stop raise Error, "Attempt to stop an interval that has not started" unless started? return false if stopped? @stop_instant = ::Hitimes.raw_instant duration end
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
.
Source
# File lib/hitimes/interval.rb, line 125 def stopped? !!@stop_instant end
returns whether or not the interval has been stopped
Private Instance Methods
Source
# File lib/hitimes/interval.rb, line 167 def calculate_duration(start, stop) (stop - start) / ::Hitimes::NANOSECONDS_PER_SECOND end
Public
↑ topAttributes
The integer representing the start instant of the Interval
. This valuea is not useful on its own. It is a platform dependent value.
The integer representing the stop instant of the Interval
. This value is not useful on its own. It is a platform dependent value.