Ruby - Using bm to make the report prettier

Introduction

bm makes the results even easier to read and provides headings for each column.

Demo

require 'benchmark' 
iterations = 1000000 #   www.  j  av  a2  s .c  o m

Benchmark.bm do |bm| 
  bm.report("for:") do 
    for i in 1..iterations do 
      x = i 
    end 
  end 
  bm.report("times:") do 
    iterations.times do |i| 
      x = i 
    end 
  end 
end

Result

bm method allows you to collect a group of benchmark tests together and display the results in a prettier way.

Related Topic