Skip to content

Instantly share code, notes, and snippets.

@pjambet
Created September 11, 2012 22:38
Show Gist options
  • Save pjambet/3702678 to your computer and use it in GitHub Desktop.
Save pjambet/3702678 to your computer and use it in GitHub Desktop.
Why I don't like method missing
require 'benchmark'
class A
def m
@foo = 1
end
end
class B
def method_missing(meth, *args, &block)
if meth == 'm'
@foo = 1
end
end
end
class C
define_method :m do
@foo = 1
end
end
a = A.new
b = B.new
c = C.new
p Benchmark.measure { 10_000_000.times { a.m } }
p Benchmark.measure { 10_000_000.times { b.m } }
p Benchmark.measure { 10_000_000.times { c.m } }
# Output :
1.300000 0.000000 1.300000 ( 1.306174)
4.170000 0.010000 4.180000 ( 4.179858)
2.030000 0.000000 2.030000 ( 2.042580)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment