Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ruby method_missing bench
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