Skip to content

Instantly share code, notes, and snippets.

@yury
Created January 6, 2011 23:33
Show Gist options
  • Save yury/768845 to your computer and use it in GitHub Desktop.
Save yury/768845 to your computer and use it in GitHub Desktop.
benchmark
require 'benchmark'
n = 1000000
words = ["string", :symbol, "string", "string", "string"]
Benchmark.bm(20) do |x|
x.report('<< "_"') do
n.times do
res = words.inject("") {|string, p| string << "#{p}_"}
end
end
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("tained") do
n.times do
res = words.map {|s| s.tainted? }
end
end
x.report("loop") do
n.times do
res = words.map {|s| s}
end
end
end
(ruby-1.9.2-p136)
⚡ ruby comp.rb
user system total real
<< "_" 4.260000 0.020000 4.280000 ( 4.475740)
join('_') 2.690000 0.000000 2.690000 ( 2.694680)
join() 4.060000 0.010000 4.070000 ( 4.070008)
tained 1.300000 0.000000 1.300000 ( 1.301931)
loop 1.020000 0.000000 1.020000 ( 1.023793)
(rbx-head)
⚡ ruby comp.rb
user system total real
<< "_" 5.090899 0.012431 5.103330 ( 5.069069)
join('_') 5.184881 0.008267 5.193148 ( 5.077835)
join() 5.289410 0.009593 5.299003 ( 5.292927)
tained 1.322216 0.007856 1.330072 ( 1.322064)
loop 1.134619 0.001723 1.136342 ( 1.125007)
(1.8.7-p174)
⚡ ruby comp.rb
user system total real
<< "_" 9.480000 0.010000 9.490000 ( 9.528440)
join('_') 3.070000 0.000000 3.070000 ( 3.097907)
join() 5.480000 0.010000 5.490000 ( 5.488951)
tained 1.980000 0.000000 1.980000 ( 1.982910)
loop 1.600000 0.000000 1.600000 ( 1.608543)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment