Permalink
Browse files

Add block param for benchmarks to allow them to do their own looping.

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...
1 parent 6588e9e commit c2843ca06f8cba2fbbf6d94a4a9568d3b7223387 @phiggins phiggins committed with Wesley Beary Dec 7, 2010
Showing with 12 additions and 2 deletions.
  1. +12 −2 lib/tach.rb
View
@@ -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
@@ -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'

0 comments on commit c2843ca

Please sign in to comment.