Skip to content

Instantly share code, notes, and snippets.

@woodrow
Created December 13, 2012 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woodrow/4274548 to your computer and use it in GitHub Desktop.
Save woodrow/4274548 to your computer and use it in GitHub Desktop.
require 'benchmark'
NUM_TRIALS = 10_000_000
Benchmark.bm(20) do |x|
x.report('no concatenation') do
NUM_TRIALS.times do
y = "onetwothree"
end
end
x.report('+') do
NUM_TRIALS.times do
y = 'one' +
'two' +
'three'
end
end
x.report('\\') do
NUM_TRIALS.times do
y = 'one' \
'two' \
'three'
end
end
x.report('<<') do
NUM_TRIALS.times do
y = 'one' <<
'two' <<
'three'
end
end
end
$ ruby benchmark_stringcat.rb
user system total real
no concatenation 1.320000 0.010000 1.330000 ( 1.332485)
+ 5.690000 0.000000 5.690000 ( 5.697842)
\ 1.380000 0.000000 1.380000 ( 1.387014)
<< 5.340000 0.000000 5.340000 ( 5.348465)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment