Created
February 16, 2013 15:07
-
-
Save kiennt/4967292 to your computer and use it in GitHub Desktop.
Pre define method
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' | |
class A | |
def method_missing name, *argv | |
super unless name =~ /^test$/ | |
"hello world" | |
end | |
end | |
class B | |
def method_missing name, *argv | |
super unless name =~ /^test$/ | |
instance_eval <<-METHOD | |
def test | |
"hello world" | |
end | |
METHOD | |
test | |
end | |
end | |
Benchmark.bm do |x| | |
a = A.new | |
b = B.new | |
n = 50000 | |
x.report("method_missing") { n.times { a.test } } | |
x.report("predefine method") { n.times { b.test } } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment