evanphx / benchmark-ips

Provides iteration per second benchmarking for Ruby
MIT License
1.72k stars 97 forks source link

Add Q&D driver method #133

Closed zenspider closed 1 month ago

zenspider commented 9 months ago

I have to look up Benchmark.ips EVERY time and the (ri) doco is still a PITA.

Pls consider a simple top level driver method like so:

def quick_bench_ips(*methods, warm:nil, time:nil, show:nil)
  Benchmark.ips do |x|
    x.warmup = warm if warm
    x.time   = time if time
    x.compare!

    if show then
      max = methods.map(&:size).max
      methods.each do |name|
        val = send name
        puts "%*s: %p" % [max, name, val]
      end
      return
    end

    methods.each do |name|
      x.report(name) do |x|
        x.times { send name }
      end
    end
  end
end

def report_tally1
  # ...
end

def report_tally2
  # ...
end

def report_tally3
  # ...
end

quick_bench_ips(:report_tally1,
                :report_tally2,
                :report_tally3)
zenspider commented 9 months ago

Ever since I scratched this up I've been driving all my benchmarks via this w/ zero arg methods for clean comparisons. It's helped clean up a lot.

eregon commented 9 months ago

send is quite a big overhead, so that should be addressed (e.g. via eval) if such a convenience method is added.

zenspider commented 9 months ago

one could say the same about block activation.

I don't care how it is implement as long as it is easy to use... but as long as it is uniform over all the benchmarks it should be fine. easy enough to crib up an empty method to see what overhead looks like.

evanphx commented 9 months ago

I think the send overhead is probably fine since the user would likely be measuring the results against previous results generated the same way. For super tight code, it might overwhelm the results but not most of the time.

@zenspider Thoughts about having the method take the x param so it can do the x.times loop? Or do you feel like that's bleeding too many details?