Skip to content

Instantly share code, notes, and snippets.

@brow
Created January 5, 2012 00:14
Show Gist options
  • Save brow/1562994 to your computer and use it in GitHub Desktop.
Save brow/1562994 to your computer and use it in GitHub Desktop.
require 'benchmark'
STRING = 'abc'
ITERATIONS = 500000
Benchmark.bm do |bench|
for n in [2,3,4]
n_strings = Array.new(n, STRING)
bench.report("add #{n}") do
ITERATIONS.times do
n_strings.reduce(:+)
end
end
bench.report("+= #{n}") do
ITERATIONS.times do
tmp = n_strings[0]
n_strings.drop(1).each{|str| tmp += str}
end
end
puts "\n"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment