public
Last active

  • Download Gist
cat.rb
Ruby
1 2 3 4 5 6 7 8 9 10
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
result.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
# 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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.