standard library But, no maintainer Which is not a big problem, given it does not need to change much with time, and it has a simple basic API But, it could be better
for i in 1..n; end } x.report("times:") { n.times {} } x.report("upto:") { 1. upto(n) {} } end user system total real for: 0.550000 0.000000 0.550000 ( 0.552241) times: 0.540000 0.000000 0.540000 ( 0.546636) upto: 0.530000 0.010000 0.540000 ( 0.552317)
bm and benchmark) item alias report Job (yielded by bmbm) item alias report list: list of items width: maximum width of items Tms operators + - * / format tms.format(’%u %r’)
“* lib/benchmark.rb: fix benchmarck to work with current ruby. patched by Benoit Daloze [ruby-core:33846] [ruby-dev:43143] merged from github.com/eregon/ruby/commits/benchmark” So it’s merged on trunk (r30747) With the first minitest/spec specs And a nice comment from Kosaki: “At minimum, your code is very clean and good readable. therefore I could find the test failure reason and fix it quickly.”
0, *labels , &blk) benchmark (...) end class Report def item(label , &blk) print label.ljust(@width) print tms = Benchmark.measure (&blk) tms # returns the measured time end end end
">avg:") do |x| f = x.report("for:") { for i in 1..n; end } t = x.report("times:") { n.times {} } u = x.report("upto:") { 1. upto(n) {} } [f+t+u, (f+t+u)/3] end user system total real for: 0.550000 0.000000 0.550000 ( 0.544705) times: 0.520000 0.000000 0.520000 ( 0.521256) upto: 0.520000 0.000000 0.520000 ( 0.519856) >total: 1.590000 0.000000 1.590000 ( 1.585816) >avg: 0.530000 0.000000 0.530000 ( 0.528605)