public
Last active

Benchmark for Symbol#to_proc and results from various Ruby implementations

  • Download Gist
to_proc_bench.rb
Ruby
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
# Benchmark code an results for:
# http://blog.gregspurrier.com/articles/relative-performance-of-symbol-to-proc-in-popular-ruby-implementations
 
require 'benchmark'
 
class Dummy
def one
1
end
end
 
puts
puts ENV['RUBY_VERSION']
puts "=" * 30
 
Benchmark.bm(25) do |b|
[10, 100, 1000].each do |array_size|
dummy_array = array_size.times.map { Dummy.new }
b.report("Symbol#to_proc (#{array_size})") do
50_000.times { dummy_array.map &:one }
end
b.report("Block (#{array_size})") do
50_000.times { dummy_array.map {|x| x.one } }
end
end
end
 
=begin
 
Benchmark results on MacBook Pro 2.66 GHz Intel Core 2 Duo
 
 
ruby-1.8.7-p302
==============================
user system total real
Symbol#to_proc (10) 0.430000 0.000000 0.430000 ( 0.437335)
Block (10) 0.190000 0.000000 0.190000 ( 0.195016)
Symbol#to_proc (100) 2.630000 0.000000 2.630000 ( 2.632784)
Block (100) 1.680000 0.010000 1.690000 ( 1.688281)
Symbol#to_proc (1000) 22.180000 0.010000 22.190000 ( 22.185858)
Block (1000) 16.570000 0.020000 16.590000 ( 16.596404)
 
ree-1.8.7-2010.02
==============================
user system total real
Symbol#to_proc (10) 0.460000 0.000000 0.460000 ( 0.463713)
Block (10) 0.190000 0.000000 0.190000 ( 0.186266)
Symbol#to_proc (100) 2.990000 0.000000 2.990000 ( 2.990879)
Block (100) 1.540000 0.010000 1.550000 ( 1.551355)
Symbol#to_proc (1000) 25.460000 0.000000 25.460000 ( 25.465169)
Block (1000) 15.290000 0.070000 15.360000 ( 15.362269)
 
ruby-1.9.2-p0
==============================
user system total real
Symbol#to_proc (10) 0.100000 0.000000 0.100000 ( 0.097246)
Block (10) 0.090000 0.000000 0.090000 ( 0.094039)
Symbol#to_proc (100) 0.720000 0.020000 0.740000 ( 0.729175)
Block (100) 0.760000 0.010000 0.770000 ( 0.773701)
Symbol#to_proc (1000) 6.920000 0.030000 6.950000 ( 6.953095)
Block (1000) 7.280000 0.020000 7.300000 ( 7.307675)
 
jruby-1.5.3
==============================
user system total real
Symbol#to_proc (10) 1.045000 0.000000 1.045000 ( 0.989000)
Block (10) 0.136000 0.000000 0.136000 ( 0.137000)
Symbol#to_proc (100) 2.200000 0.000000 2.200000 ( 2.201000)
Block (100) 0.692000 0.000000 0.692000 ( 0.692000)
Symbol#to_proc (1000) 20.252000 0.000000 20.252000 ( 20.252000)
Block (1000) 5.609000 0.000000 5.609000 ( 5.609000)
 
rbx-1.1.0-20100923
==============================
user system total real
Symbol#to_proc (10) 0.718239 0.005599 0.723838 ( 0.635084)
Block (10) 0.139432 0.000232 0.139664 ( 0.109060)
Symbol#to_proc (100) 2.348010 0.003203 2.351213 ( 2.351390)
Block (100) 0.311176 0.000240 0.311416 ( 0.311417)
Symbol#to_proc (1000) 22.866880 0.113544 22.980424 ( 22.985340)
Block (1000) 3.252945 0.095286 3.348231 ( 3.348315)
 
macruby-0.7.1
==============================
user system total real
Symbol#to_proc (10) 1.080000 0.020000 1.100000 ( 0.758175)
Block (10) 0.130000 0.000000 0.130000 ( 0.110563)
Symbol#to_proc (100) 9.150000 0.150000 9.300000 ( 6.269290)
Block (100) 0.700000 0.030000 0.730000 ( 0.639880)
Symbol#to_proc (1000) 90.920000 2.250000 93.170000 ( 61.648382)
Block (1000) 6.660000 0.390000 7.050000 ( 6.186907)
 
=end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.