From: Alex Young Date: 2007-03-07T06:52:47+09:00 Subject: Re: Automatic Benchmark Iterations Phrogz wrote: > When performing a benchmark comparison of several techniques, this > idiom is very common for me: > > N = 1_000_000 > Benchmark.bmbm{ |x| > x.report( 'foo' ){ > N.times{ > # foo code > } > } > x.report( 'bar' ){ > N.times{ > # bar code > } > } > x.report( 'jim' ){ > N.times{ > # jim code > } > } > } > > Observation #1: It's annoying to have to type "N.times{ }" for each > report. I also want the exact same value, and I always want some > iterations. > > Consequent Desire #1: It'd be nice if I could write the above as: > Benchmark.bmbm( 1_000_000 ){ |x| > x.report( 'foo' ){ > # foo code > } > x.report( 'bar' ){ > # bar code > } > x.report( 'jim' ){ > # jim code > } > } What I usually do is something like this: def foo #foo code end def bar #bar code end def moo #moo code end tests = [:foo, :bar, :moo] Benchmark.bmbm(20){|x| tests.each do |meth| x.report(meth.to_s){ for i in 0..N do self.send(meth) end } end } Avoids duplication, and makes it a little easier to check foo, bar and moo side-by-side to make sure that they're actually doing the same thing. Also makes it much easier to add extra cases. -- Alex