Skip to content

@agrimm /to_proc_bench.rb forked from gregspurrier/to_proc_bench.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Benchmark for Symbol#to_proc and results from various Ruby implementations
# Originally benchmark code and results for:
# http://blog.gregspurrier.com/articles/relative-performance-of-symbol-to-proc-in-popular-ruby-implementations
# Modified by Andrew Grimm to see if memoization could help in 1.8.7.
require 'benchmark'
class Dummy
# attr_reader is marginally faster than "real" methods
attr_reader :one
def initialize() @one = 1; end
end
BENCHMARK_REPEATS = 500_000 # 10 times greater than in the original
ARRAY_SIZES = [0, 1, 10, 100] # 10 times smaller (plus an empty one) compared to the original
PRE_EXISTING_PROC = :one.to_proc
puts
puts [ENV['RUBY_VERSION'], " ", RUBY_VERSION].join
puts "=" * 30
class Symbol
alias_method :old_to_proc, :to_proc
# Class variables are considered harmful, but I don't think
# anyone will subclass Symbol
@@proc_cache = {}
def to_proc
@@proc_cache[self] ||= old_to_proc
end
end
Benchmark.bm(30) do |b|
ARRAY_SIZES.each do |array_size|
dummy_array = array_size.times.map { Dummy.new }
b.report("Block (#{array_size})") do
i = 0
while i < BENCHMARK_REPEATS
dummy_array.map {|x| x.one }
i += 1
end
end
b.report("Pre-exing proc (#{array_size})") do
i = 0
while i < BENCHMARK_REPEATS
dummy_array.map(&PRE_EXISTING_PROC)
i += 1
end
end
b.report("New Symbol#to_proc (#{array_size})") do
i = 0
while i < BENCHMARK_REPEATS
dummy_array.map &:one
i += 1
end
end
b.report("Old Symbol#to_proc (#{array_size})") do
i = 0
while i < BENCHMARK_REPEATS
dummy_array.map(&:one.old_to_proc)
i += 1
end
end
puts
end
end
=begin
ruby-1.8.7-head 1.8.7
==============================
user system total real
Block (0) 0.350000 0.000000 0.350000 ( 0.347286)
Pre-exing proc (0) 0.380000 0.000000 0.380000 ( 0.382733)
New Symbol#to_proc (0) 0.590000 0.000000 0.590000 ( 0.588563)
Old Symbol#to_proc (0) 1.440000 0.050000 1.490000 ( 1.489680)
Block (1) 0.510000 0.000000 0.510000 ( 0.510386)
Pre-exing proc (1) 0.600000 0.000000 0.600000 ( 0.604087)
New Symbol#to_proc (1) 0.840000 0.000000 0.840000 ( 0.848143)
Old Symbol#to_proc (1) 1.700000 0.070000 1.770000 ( 1.774340)
Block (10) 1.620000 0.000000 1.620000 ( 1.623693)
Pre-exing proc (10) 2.410000 0.000000 2.410000 ( 2.428656)
New Symbol#to_proc (10) 2.650000 0.000000 2.650000 ( 2.650728)
Old Symbol#to_proc (10) 3.490000 0.010000 3.500000 ( 3.499992)
Block (100) 13.050000 0.060000 13.110000 ( 13.123321)
Pre-exing proc (100) 20.240000 0.010000 20.250000 ( 20.254187)
New Symbol#to_proc (100) 20.560000 0.010000 20.570000 ( 20.569899)
Old Symbol#to_proc (100) 21.400000 0.010000 21.410000 ( 21.425755)
kiji-head 1.8.7
==============================
user system total real
Block (0) 0.360000 0.010000 0.370000 ( 0.371986)
Pre-exing proc (0) 0.390000 0.010000 0.400000 ( 0.391153)
New Symbol#to_proc (0) 0.610000 0.000000 0.610000 ( 0.617719)
Old Symbol#to_proc (0) 1.700000 0.430000 2.130000 ( 2.131325)
Block (1) 0.550000 0.010000 0.560000 ( 0.561415)
Pre-exing proc (1) 0.620000 0.010000 0.630000 ( 0.628403)
New Symbol#to_proc (1) 0.780000 0.010000 0.790000 ( 0.792473)
Old Symbol#to_proc (1) 1.740000 0.410000 2.150000 ( 2.153028)
Block (10) 1.920000 0.070000 1.990000 ( 1.994415)
Pre-exing proc (10) 2.590000 0.090000 2.680000 ( 2.682143)
New Symbol#to_proc (10) 2.840000 0.090000 2.930000 ( 2.919281)
Old Symbol#to_proc (10) 3.980000 0.520000 4.500000 ( 4.509454)
Block (100) 13.470000 0.300000 13.770000 ( 13.780858)
Pre-exing proc (100) 22.060000 0.960000 23.020000 ( 23.020989)
New Symbol#to_proc (100) 22.360000 0.870000 23.230000 ( 23.248114)
Old Symbol#to_proc (100) 23.770000 1.600000 25.370000 ( 25.375308)
ruby 1.8.8
==============================
user system total real
Block (0) 0.350000 0.000000 0.350000 ( 0.356551)
Pre-exing proc (0) 0.390000 0.000000 0.390000 ( 0.390978)
New Symbol#to_proc (0) 0.630000 0.000000 0.630000 ( 0.624728)
Old Symbol#to_proc (0) 1.430000 0.050000 1.480000 ( 1.493316)
Block (1) 0.500000 0.000000 0.500000 ( 0.501828)
Pre-exing proc (1) 0.590000 0.000000 0.590000 ( 0.602439)
New Symbol#to_proc (1) 0.870000 0.000000 0.870000 ( 0.869580)
Old Symbol#to_proc (1) 1.670000 0.070000 1.740000 ( 1.737254)
Block (10) 1.640000 0.000000 1.640000 ( 1.645945)
Pre-exing proc (10) 2.480000 0.000000 2.480000 ( 2.477283)
New Symbol#to_proc (10) 2.750000 0.000000 2.750000 ( 2.758358)
Old Symbol#to_proc (10) 3.520000 0.010000 3.530000 ( 3.516429)
Block (100) 13.200000 0.070000 13.270000 ( 13.277518)
Pre-exing proc (100) 21.110000 0.010000 21.120000 ( 21.124276)
New Symbol#to_proc (100) 21.520000 0.010000 21.530000 ( 21.526924)
Old Symbol#to_proc (100) 22.270000 0.000000 22.270000 ( 22.287393)
ruby-1.9.2-head 1.9.2
==============================
user system total real
Block (0) 0.070000 0.000000 0.070000 ( 0.070782)
Pre-exing proc (0) 0.080000 0.000000 0.080000 ( 0.074346)
New Symbol#to_proc (0) 0.220000 0.000000 0.220000 ( 0.221901)
Old Symbol#to_proc (0) 0.100000 0.000000 0.100000 ( 0.101501)
Block (1) 0.140000 0.000000 0.140000 ( 0.143839)
Pre-exing proc (1) 0.120000 0.000000 0.120000 ( 0.119961)
New Symbol#to_proc (1) 0.300000 0.000000 0.300000 ( 0.296127)
Old Symbol#to_proc (1) 0.150000 0.000000 0.150000 ( 0.147926)
Block (10) 0.900000 0.000000 0.900000 ( 0.902817)
Pre-exing proc (10) 0.620000 0.000000 0.620000 ( 0.627699)
New Symbol#to_proc (10) 0.850000 0.000000 0.850000 ( 0.846081)
Old Symbol#to_proc (10) 0.650000 0.000000 0.650000 ( 0.653148)
Block (100) 7.590000 0.130000 7.720000 ( 7.713153)
Pre-exing proc (100) 4.830000 0.120000 4.950000 ( 4.946597)
New Symbol#to_proc (100) 5.040000 0.070000 5.110000 ( 5.103129)
Old Symbol#to_proc (100) 4.850000 0.130000 4.980000 ( 4.976693)
ruby-1.9.2-p180 1.9.2
==============================
user system total real
Block (0) 0.070000 0.000000 0.070000 ( 0.071845)
Pre-exing proc (0) 0.070000 0.000000 0.070000 ( 0.074202)
New Symbol#to_proc (0) 0.210000 0.000000 0.210000 ( 0.210908)
Old Symbol#to_proc (0) 0.100000 0.000000 0.100000 ( 0.101992)
Block (1) 0.150000 0.000000 0.150000 ( 0.145002)
Pre-exing proc (1) 0.120000 0.000000 0.120000 ( 0.120728)
New Symbol#to_proc (1) 0.250000 0.000000 0.250000 ( 0.255420)
Old Symbol#to_proc (1) 0.150000 0.000000 0.150000 ( 0.147345)
Block (10) 0.900000 0.000000 0.900000 ( 0.903972)
Pre-exing proc (10) 0.630000 0.000000 0.630000 ( 0.629392)
New Symbol#to_proc (10) 0.780000 0.000000 0.780000 ( 0.777650)
Old Symbol#to_proc (10) 0.650000 0.000000 0.650000 ( 0.651927)
Block (100) 7.620000 0.130000 7.750000 ( 7.738123)
Pre-exing proc (100) 4.780000 0.120000 4.900000 ( 4.899208)
New Symbol#to_proc (100) 4.940000 0.070000 5.010000 ( 5.039969)
Old Symbol#to_proc (100) 4.820000 0.120000 4.940000 ( 4.940571)
rbx-2.0.0pre 1.8.7
==============================
user system total real
Block (0) 0.992788 0.001931 0.994719 ( 1.101299)
Pre-exing proc (0) 1.003384 0.000580 1.003964 ( 1.004198)
New Symbol#to_proc (0) 1.645909 0.000722 1.646631 ( 1.699085)
Old Symbol#to_proc (0) 1.425532 0.001269 1.426801 ( 1.433218)
Block (1) 1.093616 0.000867 1.094483 ( 1.123901)
Pre-exing proc (1) 1.677479 0.001454 1.678933 ( 1.708643)
New Symbol#to_proc (1) 2.468520 0.001241 2.469761 ( 2.476530)
Old Symbol#to_proc (1) 2.184687 0.001290 2.185977 ( 2.203926)
Block (10) 1.637575 0.000667 1.638242 ( 1.639329)
Pre-exing proc (10) 5.238108 0.003575 5.241683 ( 5.242557)
New Symbol#to_proc (10) 6.117323 0.002523 6.119846 ( 6.121079)
Old Symbol#to_proc (10) 5.948936 0.004932 5.953868 ( 6.004936)
Block (100) 8.241922 0.004157 8.246079 ( 8.253379)
Pre-exing proc (100) 42.231079 0.017671 42.248750 ( 42.255636)
New Symbol#to_proc (100) 43.358560 0.032092 43.390652 ( 43.493051)
Old Symbol#to_proc (100) 43.528859 0.048562 43.577421 ( 44.186785)
rbx-head 1.8.7
==============================
user system total real
Block (0) 0.542246 0.003016 0.545262 ( 0.644603)
Pre-exing proc (0) 0.523996 0.000495 0.524491 ( 0.525604)
New Symbol#to_proc (0) 1.056260 0.001659 1.057919 ( 1.121293)
Old Symbol#to_proc (0) 0.749647 0.000661 0.750308 ( 0.758580)
Block (1) 0.677204 0.000257 0.677461 ( 0.718285)
Pre-exing proc (1) 0.909563 0.000957 0.910520 ( 0.942939)
New Symbol#to_proc (1) 1.543028 0.004234 1.547262 ( 1.759671)
Old Symbol#to_proc (1) 1.198826 0.002534 1.201360 ( 1.233415)
Block (10) 0.486515 0.001055 0.487570 ( 0.520401)
Pre-exing proc (10) 3.315076 0.004873 3.319949 ( 3.371097)
New Symbol#to_proc (10) 3.998407 0.006389 4.004796 ( 4.049977)
Old Symbol#to_proc (10) 3.632506 0.003363 3.635869 ( 3.654035)
Block (100) 1.867834 0.001339 1.869173 ( 1.873261)
Pre-exing proc (100) 27.176577 0.040889 27.217466 ( 27.664341)
New Symbol#to_proc (100) 28.411465 0.031482 28.442947 ( 28.764876)
Old Symbol#to_proc (100) 27.387377 0.020507 27.407884 ( 27.524016)
jruby-head 1.8.7
==============================
user system total real
Block (0) 0.342000 0.000000 0.342000 ( 0.277000)
Pre-exing proc (0) 0.123000 0.000000 0.123000 ( 0.124000)
New Symbol#to_proc (0) 0.403000 0.000000 0.403000 ( 0.403000)
Old Symbol#to_proc (0) 0.308000 0.000000 0.308000 ( 0.308000)
Block (1) 0.249000 0.000000 0.249000 ( 0.250000)
Pre-exing proc (1) 0.194000 0.000000 0.194000 ( 0.194000)
New Symbol#to_proc (1) 0.244000 0.000000 0.244000 ( 0.244000)
Old Symbol#to_proc (1) 0.300000 0.000000 0.300000 ( 0.300000)
Block (10) 0.564000 0.000000 0.564000 ( 0.564000)
Pre-exing proc (10) 0.655000 0.000000 0.655000 ( 0.655000)
New Symbol#to_proc (10) 0.722000 0.000000 0.722000 ( 0.723000)
Old Symbol#to_proc (10) 0.775000 0.000000 0.775000 ( 0.775000)
Block (100) 4.698000 0.000000 4.698000 ( 4.698000)
Pre-exing proc (100) 5.679000 0.000000 5.679000 ( 5.679000)
New Symbol#to_proc (100) 5.774000 0.000000 5.774000 ( 5.775000)
Old Symbol#to_proc (100) 5.804000 0.000000 5.804000 ( 5.804000)
jruby-head 1.9.2
==============================
user system total real
Block (0) 0.310000 0.000000 0.310000 ( 0.310000)
Pre-exing proc (0) 0.160000 0.000000 0.160000 ( 0.160000)
New Symbol#to_proc (0) 0.361000 0.000000 0.361000 ( 0.361000)
Old Symbol#to_proc (0) 0.346000 0.000000 0.346000 ( 0.346000)
Block (1) 0.274000 0.000000 0.274000 ( 0.274000)
Pre-exing proc (1) 0.206000 0.000000 0.206000 ( 0.206000)
New Symbol#to_proc (1) 0.312000 0.000000 0.312000 ( 0.313000)
Old Symbol#to_proc (1) 0.300000 0.000000 0.300000 ( 0.300000)
Block (10) 0.690000 0.000000 0.690000 ( 0.690000)
Pre-exing proc (10) 0.730000 0.000000 0.730000 ( 0.730000)
New Symbol#to_proc (10) 0.800000 0.000000 0.800000 ( 0.800000)
Old Symbol#to_proc (10) 0.808000 0.000000 0.808000 ( 0.808000)
Block (100) 5.658000 0.000000 5.658000 ( 5.658000)
Pre-exing proc (100) 5.843000 0.000000 5.843000 ( 5.843000)
New Symbol#to_proc (100) 5.966000 0.000000 5.966000 ( 5.966000)
Old Symbol#to_proc (100) 5.941000 0.000000 5.941000 ( 5.941000)
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.