Class: WonkoTheSane::Util::Benchmark

Inherits:
Object
  • Object
show all
Defined in:
lib/wonko_the_sane/util/benchmark.rb

Instance Method Summary collapse

Constructor Details

#initializeBenchmark

Returns a new instance of Benchmark.



6
7
8
9
# File 'lib/wonko_the_sane/util/benchmark.rb', line 6

def initialize
  @timers = {}
  @calls = {}
end

Instance Method Details

#benchmark(id, &block) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/wonko_the_sane/util/benchmark.rb', line 11

def benchmark(id, &block)
  @timers[id.to_s] ||= ::Benchmark::Tms.new(0, 0, 0, 0, 0, id.to_s)
  @calls[id.to_s] ||= 0
  @calls[id.to_s] += 1
  ret = nil
  @timers[id.to_s].add! { ret = yield }
  ret
end


20
21
22
23
24
25
26
# File 'lib/wonko_the_sane/util/benchmark.rb', line 20

def print_times(reset = false)
  @timers.each do |id, timer|
    puts "#{timer.label}: #{timer.real.round 2}s total, #{(timer.real / @calls[id]).round 2}s/call"
  end
  @timers = {} if reset
  @calls = {} if reset
end