Last active
August 29, 2015 14:15
-
-
Save palkan/c1ae6f56b8e718e7941f to your computer and use it in GitHub Desktop.
Ruby method_missing bench
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
_methods = ("a".."z").to_a | |
module A | |
module B | |
module C | |
module D | |
module E | |
class X | |
attr_accessor *("a".."z").to_a | |
def method_missing(name) | |
self.send(name[1..-1]) | |
end | |
end | |
end | |
end | |
end | |
end | |
end | |
class X2 | |
attr_accessor *("a".."z").to_a | |
def method_missing(name) | |
self.send(name[1..-1]) | |
end | |
end | |
n = 1000000 | |
Benchmark.bm do |x| | |
x.report { n.times do; X2.new.send("_#{_methods.sample}"); end } | |
x.report { n.times do; A::B::C::D::E::X.new.send("_#{_methods.sample}"); end } | |
x.report { n.times do; A::B::C::D::E::X.new.send(_methods.sample); end } | |
end | |
# user system total real | |
# 0.950000 0.000000 0.950000 (0.950808) | |
# 0.980000 0.000000 0.980000 (0.985629) | |
# 0.370000 0.000000 0.370000 (0.372899) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment