wikiti
1/22/2018 - 11:39 AM

Benchmark Ruby code

Benchmark some code using Ruby's core utils.

require 'benchmark'

# Measure execution time
puts Benchmark.measure { 'a' * 1_000_000 }

# Benchmark comparisson
Benchmark.bm do |bm|
  iterations = 100_000

  bm.report 'Using #join' do
    iterations.times do
      ["The", "current", "time", "is", Time.now].join(" ")
    end
  end

  bm.report 'Using interpolation' do
    iterations.times do
      "The current time is #{Time.now}"
    end
  end
end