Skip to content

Instantly share code, notes, and snippets.

@thisivan
Created October 13, 2011 16:41
Show Gist options
  • Save thisivan/1284745 to your computer and use it in GitHub Desktop.
Save thisivan/1284745 to your computer and use it in GitHub Desktop.
String Concatenation
# Ruby String Concatenation Benchmarks
require 'benchmark'
n = 100000
a = "a"
b = "b"
c = "c"
Benchmark.bm do |x|
x.report { for i in 1..n ; str = ""; str << a << b << c ; end }
x.report { for i in 1..n ; str = ""; str = a + b + c ; end }
x.report { for i in 1..n ; str = ""; str = "#{a}#{b}#{c}" ; end }
x.report { for i in 1..n ; str = ""; str = [a,b,c].join ; end }
end
# Ruby 1.9.2
# user system total real
# 0.050000 0.000000 0.050000 ( 0.057254)
# 0.060000 0.000000 0.060000 ( 0.061110)
# 0.060000 0.000000 0.060000 ( 0.066547)
# 0.120000 0.000000 0.120000 ( 0.123455)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment