Class: Hitimes::Interval
- Inherits:
-
Object
- Object
- Hitimes::Interval
- 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
-
#start_instant ⇒ Object
readonly
Public: The integer representing the start instant of the Interval.
-
#stop_instant ⇒ Object
readonly
Public: The integer representing the stop instant of the Interval.
Class Method Summary collapse
-
.measure ⇒ Object
call-seq: Interval.measure { } -> Float.
-
.now ⇒ Object
call-seq: Interval.now -> Interval.
Instance Method Summary collapse
-
#duration ⇒ Object
(also: #to_f, #to_seconds, #length)
call-seq: interval.duration -> Float interval.to_f -> Float interval.to_seconds -> Float interval.length -> Float.
-
#duration_so_far ⇒ Object
call-seq: interval.duration_so_far -> Float or false.
-
#initialize(start = nil, stop = nil) ⇒ Interval
constructor
A new instance of Interval.
-
#running? ⇒ Boolean
call-seq: interval.running? -> boolean.
-
#split ⇒ Object
call-seq: interval.split -> Interval.
-
#start ⇒ Object
call-seq: interval.start -> boolean.
-
#started? ⇒ Boolean
call-seq: interval.started? -> boolean.
-
#stop ⇒ Object
call-seq: interval.stop -> bool or Float.
-
#stopped? ⇒ Boolean
call-seq: interval.stopped? -> boolean.
Constructor Details
#initialize(start = nil, stop = nil) ⇒ Interval
Returns a new instance of Interval.
33 34 35 36 37 |
# File 'lib/hitimes/interval.rb', line 33 def initialize(start = nil, stop = nil) @start_instant = start @stop_instant = stop @duration = -Float::INFINITY end |
Instance Attribute Details
#start_instant ⇒ Object (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.
27 28 29 |
# File 'lib/hitimes/interval.rb', line 27 def start_instant @start_instant end |
#stop_instant ⇒ Object (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.
31 32 33 |
# File 'lib/hitimes/interval.rb', line 31 def stop_instant @stop_instant end |
Class Method Details
.measure ⇒ Object
call-seq:
Interval.measure { } -> Float
Times the execution of the block returning the number of seconds it took
52 53 54 55 56 57 58 59 60 |
# 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 |
.now ⇒ Object
call-seq:
Interval.now -> Interval
Create an interval that has already started
44 45 46 |
# File 'lib/hitimes/interval.rb', line 44 def self.now Interval.new(Hitimes.raw_instant) end |
Instance Method Details
#duration ⇒ Object 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.
151 152 153 154 155 156 157 158 159 |
# 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 |
#duration_so_far ⇒ Object
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.
106 107 108 109 110 111 |
# 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 |
#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.
135 136 137 |
# File 'lib/hitimes/interval.rb', line 135 def running? started? && !stopped? end |
#split ⇒ Object
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.
67 68 69 70 |
# File 'lib/hitimes/interval.rb', line 67 def split @stop_instant = ::Hitimes.raw_instant Interval.new(@stop_instant) end |
#start ⇒ Object
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
.
78 79 80 81 82 83 |
# File 'lib/hitimes/interval.rb', line 78 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
117 118 119 |
# File 'lib/hitimes/interval.rb', line 117 def started? !!@start_instant end |
#stop ⇒ Object
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
.
91 92 93 94 95 96 97 98 |
# 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 |
#stopped? ⇒ Boolean
call-seq:
interval.stopped? -> boolean
returns whether or not the interval has been stopped
125 126 127 |
# File 'lib/hitimes/interval.rb', line 125 def stopped? !!@stop_instant end |