Skip to content

Instantly share code, notes, and snippets.

@toshimaru
Created July 4, 2017 01:12
Show Gist options
  • Save toshimaru/198a714f48517fa630f3476757d67a57 to your computer and use it in GitHub Desktop.
Save toshimaru/198a714f48517fa630f3476757d67a57 to your computer and use it in GitHub Desktop.
require 'benchmark/ips'
Benchmark.ips do |x|
ary = []
x.report(:a) {
10_000.times do |num|
ary << "#{num} #{num * 2} #{num * 3}"
end
}
x.report(:b){
calc_num = -> (n) { "#{n} #{n * 2} #{n * 3}" }
10_000.times do |num|
ary << calc_num.call(num)
end
}
x.compare!
end
Warming up --------------------------------------
a 13.000 i/100ms
b 10.000 i/100ms
Calculating -------------------------------------
a 109.073 (±22.9%) i/s - 468.000 in 5.057635s
b 81.887 (±41.5%) i/s - 300.000 in 5.174320s
Comparison:
a: 109.1 i/s
b: 81.9 i/s - same-ish: difference falls within error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment