Skip to content

Instantly share code, notes, and snippets.

@yury
Created Aug 24, 2010
Embed
What would you like to do?
require 'benchmark'
n = 1000000
words = ["string", :symbol]
Benchmark.bm(20) do |x|
x.report("join('_')") do
n.times do
res = words.map{|s| s}.join("_")
end
end
x.report("join()") do
n.times do
res = words.map{|s| "#{s}_"}.join
end
end
x.report('<< "_"') do
n.times do
res = words.inject("") {|string, p| string << "#{p}_"}
end
end
end
rvm system,1.8.7,rbx-head,1.9.2 comp.rb
info: system: ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]
user system total real
join('_') 2.200000 0.000000 2.200000 ( 2.272801)
join() 3.140000 0.010000 3.150000 ( 3.166814)
<< "_" 4.660000 0.000000 4.660000 ( 4.678321)
info: 1.8.7 (ruby-1.8.7-p302): ruby 1.8.7 (2010-08-16 patchlevel 302) [i686-darwin10.4.0]
user system total real
join('_') 2.390000 0.000000 2.390000 ( 2.390423)
join() 3.250000 0.000000 3.250000 ( 3.270108)
<< "_" 4.620000 0.000000 4.620000 ( 4.632467)
info: rbx-head: rubinius 1.0.1 (1.8.7 0d440bb0 2010-06-03 JI) [x86_64-apple-darwin10.4.0]
user system total real
join('_') 6.053744 0.016591 6.070335 ( 5.805713)
join() 5.661479 0.010880 5.672359 ( 5.663749)
<< "_" 3.408194 0.009164 3.417358 ( 3.390956)
info: 1.9.2 (ruby-1.9.2-p0): ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.4.0]
user system total real
join('_') 1.760000 0.000000 1.760000 ( 1.756008)
join() 2.170000 0.010000 2.180000 ( 2.188050)
<< "_" 1.990000 0.000000 1.990000 ( 1.995551)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment