Class: Timeit::Timer
- Inherits:
-
Object
- Object
- Timeit::Timer
- Defined in:
- lib/timeit.rb
Constant Summary collapse
- IllegalStateError =
Class.new(RuntimeError)
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#start_time ⇒ Object
readonly
Returns the value of attribute start_time.
Instance Method Summary collapse
- #duration ⇒ Object
-
#initialize ⇒ Timer
constructor
A new instance of Timer.
- #rate ⇒ Object
- #reset_split! ⇒ Object
- #split ⇒ Object
- #split_rate ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #tick!(count = 1) ⇒ Object
- #total_duration ⇒ Object
Constructor Details
#initialize ⇒ Timer
Returns a new instance of Timer.
12 13 14 15 16 17 18 19 20 |
# File 'lib/timeit.rb', line 12 def initialize @start_time = nil @count = 0 @rate = 0 @split_time = nil @split_count = 0 @split_rate = 0 end |
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
10 11 12 |
# File 'lib/timeit.rb', line 10 def count @count end |
#start_time ⇒ Object (readonly)
Returns the value of attribute start_time.
10 11 12 |
# File 'lib/timeit.rb', line 10 def start_time @start_time end |
Instance Method Details
#duration ⇒ Object
30 31 32 33 34 |
# File 'lib/timeit.rb', line 30 def duration duration = Time.now - @start_time @rate = @count / duration duration end |
#rate ⇒ Object
63 64 65 |
# File 'lib/timeit.rb', line 63 def rate @rate end |
#reset_split! ⇒ Object
54 55 56 |
# File 'lib/timeit.rb', line 54 def reset_split! @split_time = @start_time end |
#split ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/timeit.rb', line 44 def split split = Time.now - @split_time @split_time = Time.now @split_rate = @split_count / split @split_count = 0 split end |
#split_rate ⇒ Object
67 68 69 |
# File 'lib/timeit.rb', line 67 def split_rate @split_rate end |
#start ⇒ Object
22 23 24 |
# File 'lib/timeit.rb', line 22 def start @start_time = @split_time = Time.now end |
#stop ⇒ Object
26 27 28 |
# File 'lib/timeit.rb', line 26 def stop @stop_time = Time.now end |
#tick!(count = 1) ⇒ Object
58 59 60 61 |
# File 'lib/timeit.rb', line 58 def tick!(count = 1) @count += count @split_count += count end |
#total_duration ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/timeit.rb', line 36 def total_duration if @stop_time @stop_time - @start_time else raise(IllegalStateError, "Timer must be stopped for total duration") end end |