Skip to content

Instantly share code, notes, and snippets.

@thisivan
Created August 24, 2011 09:19
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 thisivan/1167670 to your computer and use it in GitHub Desktop.
Save thisivan/1167670 to your computer and use it in GitHub Desktop.
Method Missing vs Method Compilation
require 'benchmark'
# Defined method
class ExampleA
def test
10000 * 10000
end
end
# Method lookup
class ExampleB < ExampleA
end
# Method Missing
class ExampleC
def method_missing(name, *args, &block)
10000 * 10000
end
end
class ExampleD
end
ExampleD.class_eval do
def test
10000 * 10000
end
end
Benchmark.bm do |b|
b.report { 100000.times { ExampleA.new.test }}
b.report { 100000.times { ExampleB.new.test }}
b.report { 100000.times { ExampleC.new.test }}
b.report { 100000.times { ExampleD.new.test }}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment