Skip to content

Instantly share code, notes, and snippets.

@spraints
Created June 8, 2012 12:13
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save spraints/2895311 to your computer and use it in GitHub Desktop.
require 'benchmark'
puts "#{RUBY_ENGINE rescue ''} #{RUBY_ENGINE == 'jruby' ? JRUBY_VERSION : RUBY_VERSION}"
def a ; "abc"*100 ; end
def b ; "def"*100 ; end
n = 1000000
Benchmark.bm(12) do |x|
x.report('"#{a}#{b}"') { n.times { "#{a}#{b}" } }
x.report('"" + a + b') { n.times { "" + a + b } }
x.report('"" << a << b') { n.times { "" << a << b } }
end
# rvm all do ruby cat.rb
jruby 1.3.1
user system total real
"#{a}#{b}" 1.405000 0.000000 1.405000 ( 1.351000)
"" + a + b 0.920000 0.000000 0.920000 ( 0.920000)
"" << a << b 0.908000 0.000000 0.908000 ( 0.908000)
jruby 1.6.1
user system total real
"#{a}#{b}" 1.150000 0.000000 1.150000 ( 1.108000)
"" + a + b 0.984000 0.000000 0.984000 ( 0.984000)
"" << a << b 0.952000 0.000000 0.952000 ( 0.953000)
jruby 1.6.4
user system total real
"#{a}#{b}" 1.198000 0.000000 1.198000 ( 1.147000)
"" + a + b 0.903000 0.000000 0.903000 ( 0.903000)
"" << a << b 0.902000 0.000000 0.902000 ( 0.902000)
ruby 1.8.7
user system total real
"#{a}#{b}" 4.040000 0.010000 4.050000 ( 4.069462)
"" + a + b 4.440000 0.180000 4.620000 ( 4.660303)
"" << a << b 4.240000 0.010000 4.250000 ( 4.261220)
stringcat.rb:2: uninitialized constant RUBY_ENGINE (NameError)
stringcat.rb:2: uninitialized constant RUBY_ENGINE (NameError)
ruby 1.9.2
user system total real
"#{a}#{b}" 2.460000 0.210000 2.670000 ( 2.681895)
"" + a + b 2.480000 0.120000 2.600000 ( 2.596685)
"" << a << b 2.530000 0.190000 2.720000 ( 2.718471)
ruby 1.9.2
user system total real
"#{a}#{b}" 2.160000 0.160000 2.320000 ( 2.320706)
"" + a + b 2.280000 0.180000 2.460000 ( 2.470008)
"" << a << b 2.460000 0.170000 2.630000 ( 2.632798)
ruby 1.9.3
user system total real
"#{a}#{b}" 2.540000 0.150000 2.690000 ( 2.698078)
"" + a + b 2.740000 0.180000 2.920000 ( 2.937345)
"" << a << b 2.760000 0.260000 3.020000 ( 3.019918)
ruby 1.9.3
user system total real
"#{a}#{b}" 2.630000 0.150000 2.780000 ( 2.790978)
"" + a + b 2.790000 0.200000 2.990000 ( 2.993584)
"" << a << b 2.880000 0.240000 3.120000 ( 3.145599)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment