Skip to content

Commit

Permalink
Add block param for benchmarks to allow them to do their own looping.
Browse files Browse the repository at this point in the history
This helps in the case that a benchmark had some expensive setup. Previously,
* The setup could be done once, outside the benchmark:

  expensive_setup()
  tach("method1") do
    # code to benchmark
  end

* The setup could be done every test, skewing the benchmark:

  tach("method2") do
    expensive_setup()
    # code to benchmark
  end

With this change, you have a third option.

* The setup is done once, and the benchmark loops itself:
  tach("method3") do |n|
    expensive_setup()
    n.times do
      # code to benchmark
    end
  end

One unfortunate side-effect of this is that using the new syntax will result in
lower benchmark scores because of avoiding the overhead of calling a block
multiple times.
  • Loading branch information
phiggins committed Dec 7, 2010
1 parent 5d018f3 commit 04c4626
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/tach.rb
Expand Up @@ -54,9 +54,13 @@ def run_tach(name, count, &benchmark)
print(name)
STDOUT.flush
tach_start = Time.now
for index in 1..count
instance_eval(&benchmark)

if benchmark.arity == 0
count.times { benchmark.call }
else
benchmark.call(count)
end

tach_finish = Time.now
duration = tach_finish.to_f - tach_start.to_f
duration
Expand Down Expand Up @@ -90,6 +94,12 @@ def run_tach(name, count, &benchmark)
[$1, $2]
end

tach('regex 4') do |n|
n.times do
header = data.match(/(.*):\s(.*)/)
[$1, $2]
end
end
end

require 'benchmark'
Expand Down

0 comments on commit 04c4626

Please sign in to comment.