Class: Timeit::Timer

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

Constant Summary collapse

IllegalStateError =
Class.new(RuntimeError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTimer

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

#countObject (readonly)

Returns the value of attribute count.



10
11
12
# File 'lib/timeit.rb', line 10

def count
  @count
end

#start_timeObject (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

#durationObject



30
31
32
33
34
# File 'lib/timeit.rb', line 30

def duration
  duration = Time.now - @start_time
  @rate = @count / duration
  duration
end

#rateObject



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

#splitObject



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_rateObject



67
68
69
# File 'lib/timeit.rb', line 67

def split_rate
  @split_rate
end

#startObject



22
23
24
# File 'lib/timeit.rb', line 22

def start
  @start_time = @split_time = Time.now
end

#stopObject



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_durationObject



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