Created
October 21, 2011 18:29
-
-
Save valo/1304570 to your computer and use it in GitHub Desktop.
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
class MethodMissing | |
def method_missing(name, *args) | |
msg = name.to_s[/print_(.*)/, 1] | |
print msg | |
end | |
end | |
class LazyMethodDefine | |
def method_missing(name, *args) | |
msg = name.to_s[/print_(.*)/, 1] | |
self.class.send(:define_method, name) do | |
print msg | |
end | |
send name | |
end | |
end | |
require 'benchmark' | |
puts Benchmark.measure { 100000.times { MethodMissing.new.print_h } } # => 0.760000 0.130000 0.890000 ( 0.890088) | |
puts Benchmark.measure { 100000.times { LazyMethodDefine.new.print_h } } # => 0.310000 0.100000 0.410000 ( 0.413843) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment