Skip to content

Instantly share code, notes, and snippets.

@palkan
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save palkan/c1ae6f56b8e718e7941f to your computer and use it in GitHub Desktop.
Save palkan/c1ae6f56b8e718e7941f to your computer and use it in GitHub Desktop.
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