Class: Quicky::Timer

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

Instance Method Summary collapse

Constructor Details

#initializeTimer

Returns a new instance of Timer.



7
8
9
# File 'lib/quicky/timer.rb', line 7

def initialize
  @collector = ResultsHash.new
end

Instance Method Details

#loop(name, x, options = {}, &blk) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/quicky/timer.rb', line 11

def loop(name, x, options={}, &blk)
  if options[:warmup]
    options[:warmup].times do |i|
      #puts "Warming up... #{i}"
      yield
    end
  end
  x.times do |i|
    time(name, options, &blk)
  end
end

#loop_for(name, seconds, options = {}, &blk) ⇒ Object

will loop for number of seconds



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/quicky/timer.rb', line 24

def loop_for(name, seconds, options={}, &blk)
  end_at = Time.now + seconds
  if options[:warmup]
    options[:warmup].times do |i|
      #puts "Warming up... #{i}"
      yield
    end
  end
  while Time.now < end_at
    time(name, options, &blk)
  end
end

#results(name = nil) ⇒ Object



48
49
50
51
52
53
# File 'lib/quicky/timer.rb', line 48

def results(name=nil)
  if name
    return @collector[name]
  end
  @collector
end

#time(name, options = {}, &blk) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/quicky/timer.rb', line 37

def time(name, options={}, &blk)
  t = Time.now
  yield
  duration = Time.now.to_f - t.to_f
  #puts 'duration=' + duration.to_s
  r = TimeResult.new(duration)
  @collector[name] ||= TimeCollector.new(name)
  @collector[name] << r
  r
end