Skip to content

Instantly share code, notes, and snippets.

@mediocretes
Created September 12, 2011 20:30
Show Gist options
  • Save mediocretes/1212317 to your computer and use it in GitHub Desktop.
Save mediocretes/1212317 to your computer and use it in GitHub Desktop.
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