Last active
December 18, 2015 14:49
-
-
Save jgaskins/5800177 to your computer and use it in GitHub Desktop.
Proxy object
This file contains hidden or 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 FooProxy | |
attr_reader :foo | |
def initialize(foo) | |
@foo = foo | |
end | |
def bar | |
foo.bar + baz | |
end | |
def baz | |
"Baz" | |
end | |
def method_missing(*args) | |
foo.public_send(*args) | |
end | |
end | |
module FooDCI | |
def bar | |
baz + super | |
end | |
def baz | |
"Baz" | |
end | |
end | |
class Foo | |
def bar | |
"Bar" | |
end | |
def quux | |
"Quux" | |
end | |
end | |
Benchmark.bmbm do |x| | |
iterations = 100_000 | |
x.report('Proxy') { iterations.times { FooProxy.new(Foo.new).bar } } | |
x.report('Proxy method_missing') { iterations.times { FooProxy.new(Foo.new).quux } } | |
x.report('extend') { iterations.times { Foo.new.extend(FooDCI).bar } } | |
x.report('extend using quux') { iterations.times { Foo.new.extend(FooDCI).quux } } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results in Rubinius, JRuby and MRI 2.0: