Created
September 12, 2011 20:30
-
-
Save mediocretes/1212317 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
require 'Benchmark' | |
class OldFashioned | |
def self.call(i) | |
x = 2+i | |
end | |
end | |
class NewFangled | |
def self.call(i) | |
x = Fangler.new.add i | |
y = Fangler.new.bar i | |
end | |
class Fangler | |
ALLOWED = [:add, :other, :more, :different, :wut] | |
def method_missing(method, *args, &block) | |
Adder.send(method, *args) if ALLOWED.include? method | |
end | |
end | |
class Adder | |
def self.add(i) | |
x = 2 + i | |
end | |
def self.bar(i) | |
x = 3* i | |
end | |
end | |
end | |
times = 10000 | |
time = Benchmark.realtime do | |
(1..times).each { |i| | |
OldFashioned.call i | |
} | |
end | |
puts "elapsed #{time*1000} ms" | |
time = Benchmark.realtime do | |
(1..times).each { |i| | |
NewFangled.call i | |
} | |
end | |
puts "elapsed #{time*1000} ms" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment