Class: Hitimes::Interval

Inherits:
Object
  • Object
show all
Defined in:
lib/hitimes/interval.rb

Overview

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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start = nil, stop = nil) ⇒ Interval

Returns a new instance of Interval.


32
33
34
35
36
# File 'lib/hitimes/interval.rb', line 32

def initialize(start = nil, stop = nil)
  @start_instant = start
  @stop_instant  = stop
  @duration      = -Float::INFINITY
end

Instance Attribute Details

#start_instantObject (readonly)

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


26
27
28
# File 'lib/hitimes/interval.rb', line 26

def start_instant
  @start_instant
end

#stop_instantObject (readonly)

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


30
31
32
# File 'lib/hitimes/interval.rb', line 30

def stop_instant
  @stop_instant
end

Class Method Details

.measure(&block) ⇒ Object

call-seq:

Interval.measure {  }  -> Float

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

Raises:


51
52
53
54
55
56
57
58
59
# 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

.nowObject

call-seq:

Interval.now -> Interval

Create an interval that has already started


43
44
45
# File 'lib/hitimes/interval.rb', line 43

def self.now
  Interval.new(Hitimes.raw_instant)
end

Instance Method Details

#durationObject Also known as: to_f, to_seconds, length

call-seq:

interval.duration -> Float
interval.to_f -> Float
interval.to_seconds -> Float
interval.length -> Float

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.

Raises:


149
150
151
152
153
154
155
156
157
158
159
# 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

#duration_so_farObject

call-seq:

interval.duration_so_far -> Float or false

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.


104
105
106
107
108
109
# 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

#running?Boolean

call-seq:

interval.running? -> boolean

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

Returns:

  • (Boolean)

133
134
135
# File 'lib/hitimes/interval.rb', line 133

def running?
  started? && !stopped?
end

#splitObject

call-seq:

interval.split -> Interval

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


66
67
68
69
# File 'lib/hitimes/interval.rb', line 66

def split
  @stop_instant = ::Hitimes.raw_instant
  return Interval.new(@stop_instant)
end

#startObject

call-seq:

interval.start -> boolean

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.


77
78
79
80
81
# File 'lib/hitimes/interval.rb', line 77

def start
  return false if started?
  @start_instant = ::Hitimes.raw_instant
  true
end

#started?Boolean

call-seq:

interval.started? -> boolean

returns whether or not the interval has been started

Returns:

  • (Boolean)

115
116
117
# File 'lib/hitimes/interval.rb', line 115

def started?
  !!@start_instant
end

#stopObject

call-seq:

interval.stop -> bool or Float

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.

Raises:


89
90
91
92
93
94
95
96
# 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

call-seq:

interval.stopped? -> boolean

returns whether or not the interval has been stopped

Returns:

  • (Boolean)

123
124
125
# File 'lib/hitimes/interval.rb', line 123

def  stopped?
  !!@stop_instant
end