Skip to content

Instantly share code, notes, and snippets.

@brixen
Created January 6, 2011 23:55
Show Gist options
  • Save brixen/768870 to your computer and use it in GitHub Desktop.
Save brixen/768870 to your computer and use it in GitHub Desktop.
require 'benchmark'
n = (ARGV.shift || 1000000).to_i
def append(words, n)
n.times { res = words.inject("") {|string, p| string << "#{p}_"} }
end
def join_under(words, n)
n.times { res = words.map{|s| s}.join("_") }
end
def join(words, n)
n.times { res = words.map{|s| "#{s}_"}.join }
end
def tainted(words, n)
n.times { res = words.map {|s| s.tainted? } }
end
def empty_loop(words, n)
n.times { res = words.map {|s| s} }
end
words = ["string", :symbol, "string", "string", "string"]
Benchmark.bmbm(20) do |x|
x.report('<< "_"') do
append words, n
end
x.report("join('_')") do
join_under words, n
end
x.report("join()") do
join words, n
end
x.report("tainted?") do
tainted words, n
end
x.report("loop") do
empty_loop words, n
end
end
gauss:rubinius brian$ ruby1.8.7 -v bm_array.rb
ruby 1.8.7 (2010-12-23 patchlevel 330) [i686-darwin9.8.0]
Rehearsal -------------------------------------------------------
<< "_" 8.120000 0.020000 8.140000 ( 8.227373)
join('_') 2.870000 0.010000 2.880000 ( 2.933932)
join() 4.980000 0.020000 5.000000 ( 5.045320)
tainted? 1.830000 0.010000 1.840000 ( 1.856563)
loop 1.480000 0.000000 1.480000 ( 1.505634)
--------------------------------------------- total: 19.340000sec
user system total real
<< "_" 8.070000 0.020000 8.090000 ( 8.171541)
join('_') 2.880000 0.010000 2.890000 ( 2.894225)
join() 4.990000 0.010000 5.000000 ( 5.063031)
tainted? 1.830000 0.000000 1.830000 ( 1.852958)
loop 1.480000 0.010000 1.490000 ( 1.483677)
gauss:rubinius brian$ ruby1.9.2 -v bm_array.rb
ruby 1.9.2p136 (2010-12-25 revision 30365) [i386-darwin9.8.0]
Rehearsal -------------------------------------------------------
<< "_" 4.510000 0.020000 4.530000 ( 4.609053)
join('_') 2.710000 0.010000 2.720000 ( 2.745341)
join() 4.000000 0.020000 4.020000 ( 4.080192)
tainted? 1.530000 0.010000 1.540000 ( 1.551690)
loop 1.170000 0.000000 1.170000 ( 1.177142)
--------------------------------------------- total: 13.980000sec
user system total real
<< "_" 4.500000 0.020000 4.520000 ( 4.572382)
join('_') 2.710000 0.010000 2.720000 ( 2.840988)
join() 4.010000 0.020000 4.030000 ( 4.080698)
tainted? 1.530000 0.010000 1.540000 ( 1.558215)
loop 1.170000 0.000000 1.170000 ( 1.390231)
gauss:rubinius brian$ rbx -v bm_array.rb
rubinius 1.2.1dev (1.8.7 52bb1a5b 2010-12-21 JI) [i686-apple-darwin9.8.0]
Rehearsal -------------------------------------------------------
<< "_" 4.719420 0.024558 4.743978 ( 4.771132)
join('_') 4.988875 0.027527 5.016402 ( 4.931477)
join() 4.760687 0.018208 4.778895 ( 4.807103)
tainted? 1.301048 0.005075 1.306123 ( 1.304868)
loop 1.101795 0.004335 1.106130 ( 1.097168)
--------------------------------------------- total: 16.951528sec
user system total real
<< "_" 4.469994 0.017340 4.487334 ( 4.564750)
join('_') 4.368997 0.015696 4.384693 ( 4.475056)
join() 4.706614 0.016826 4.723440 ( 4.787101)
tainted? 1.339720 0.005845 1.345565 ( 1.322900)
loop 1.073459 0.003020 1.076479 ( 1.083705)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment