Class: WonkoTheSane::Util::Benchmark
- Inherits:
-
Object
- Object
- WonkoTheSane::Util::Benchmark
- Defined in:
- lib/wonko_the_sane/util/benchmark.rb
Instance Method Summary collapse
- #benchmark(id, &block) ⇒ Object
-
#initialize ⇒ Benchmark
constructor
A new instance of Benchmark.
- #print_times(reset = false) ⇒ Object
Constructor Details
#initialize ⇒ Benchmark
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 |
#print_times(reset = false) ⇒ Object
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 |