Benchmark::Report.#reportとBenchmark.#measure

Benchmark::Repot.#reportのコード

require 'benchmark'

puts Benchmark::CAPTION
puts Benchmark.measure { "a"*1_000_000 }

=>

    user     system      total        real
1.166667   0.050000   1.216667 (  0.571355)

Benchmark.#measureのコード

require 'benchmark'

n = 50000
Benchmark.bm do |x|
  x.report { for i in 1..n; a = "1"; end }
  x.report { n.times do   ; a = "1"; end }
  x.report { 1.upto(n) do ; a = "1"; end }
end

=>

      user     system      total        real
  1.033333   0.016667   1.016667 (  0.492106)
  1.483333   0.000000   1.483333 (  0.694605)
  1.516667   0.000000   1.516667 (  0.711077)

#report、#measureではBenchmark::Tmsオブジェクトが返ってくる。Reportでは実行結果が標準出力されるが、#measureではそれがないのが大きな違い。Tmsを使うことでその後の処理が自由になるのが特徴。