End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
class method
Benchmark.bm
bm(label_width = 0, *labels) { |report| ... }
lib/benchmark.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.0.5/lib/benchmark.rb, line 204
def bm(label_width = 0, *labels, &blk) # :yield: report
benchmark(CAPTION, label_width, FORMAT, *labels, &blk)
end
A simple interface to the benchmark method, bm generates sequential reports with labels. label_width and labels parameters have the same meaning as for benchmark.
Example
require 'benchmark' n = 5000000 Benchmark.bm(7) do |x| x.report("for:") { for i in 1..n; a = "1"; end } x.report("times:") { n.times do ; a = "1"; end } x.report("upto:") { 1.upto(n) do ; a = "1"; end } end
Generates:
user system total real for: 0.960000 0.000000 0.960000 ( 0.957966) times: 0.960000 0.000000 0.960000 ( 0.960423) upto: 0.950000 0.000000 0.950000 ( 0.954864)