Skip to content

Instantly share code, notes, and snippets.

@gabetax
Created February 13, 2013 19:08
Show Gist options
  • Save gabetax/4947200 to your computer and use it in GitHub Desktop.
Save gabetax/4947200 to your computer and use it in GitHub Desktop.
ruby String#+ vs interpolation benchmark
require 'faker'
require 'benchmark'
times = 10000000
str = 'hello world'
Benchmark.bm do |b|
b.report("+ with space") do
times.times do
str + str
end
end
b.report("+ without space") do
times.times do
str + ' ' + str
end
end
b.report("interpolation") do
times.times do
"#{str}#{str}"
end
end
end
@gabetax
Copy link
Author

gabetax commented Feb 13, 2013

       user     system      total        real
+ with space  3.110000   0.030000   3.140000 (  3.166537)
+ without space  9.210000   0.050000   9.260000 (  9.274476)
interpolation  3.490000   0.020000   3.510000 (  3.525237)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment