IRB Benchmark Helper Method

Quick and dirty ruby benchmarking helper that you can stick in your .irbrc:

require 'benchmark'
def benchmark(n, &block)
  Benchmark.bm do |x|
    x.report do
      n.times { block.call }
    end
  end
end

Then call it like so:

str = "2013-01-21"
benchmark(10) do
  Date.parse(str)
end
benchmark(10) { Date.strptime(str, "%Y-%m-%d") }

Simples!

Leave a Comment

Filed under programming, ruby