Skip to content

Instantly share code, notes, and snippets.

@jgaskins
Last active December 18, 2015 14:49
Show Gist options
  • Save jgaskins/5800177 to your computer and use it in GitHub Desktop.
Save jgaskins/5800177 to your computer and use it in GitHub Desktop.
Proxy object
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
@jgaskins
Copy link
Author

Results in Rubinius, JRuby and MRI 2.0:

➜  Code  rvm rbx do rbx benchmark_proxy_objects.rb
Rehearsal --------------------------------------------------------
Proxy                  1.354664   0.015928   1.370592 (  0.795843)
Proxy method_missing   0.378131   0.004161   0.382292 (  0.214804)
extend                 5.989122   0.143207   6.132329 (  4.607476)
extend using quux      3.389954   0.087350   3.477304 (  3.469409)
---------------------------------------------- total: 11.362517sec

                           user     system      total        real
Proxy                  0.178767   0.001212   0.179979 (  0.174005)
Proxy method_missing   0.069720   0.000682   0.070402 (  0.072454)
extend                 3.802052   0.089413   3.891465 (  3.960050)
extend using quux      3.109214   0.072562   3.181776 (  3.218772)
➜  Code  rvm jruby do jruby benchmark_proxy_objects.rb
Rehearsal --------------------------------------------------------
Proxy                  1.290000   0.020000   1.310000 (  0.779000)
Proxy method_missing   1.020000   0.050000   1.070000 (  0.643000)
extend                 3.900000   0.180000   4.080000 (  2.868000)
extend using quux      1.020000   0.040000   1.060000 (  0.903000)
----------------------------------------------- total: 7.520000sec

                           user     system      total        real
Proxy                  0.080000   0.000000   0.080000 (  0.068000)
Proxy method_missing   0.100000   0.000000   0.100000 (  0.087000)
extend                 0.900000   0.020000   0.920000 (  0.715000)
extend using quux      0.850000   0.020000   0.870000 (  0.586000)
➜  Code  rvm 2.0 do ruby benchmark_proxy_objects.rb
Rehearsal --------------------------------------------------------
Proxy                  0.130000   0.000000   0.130000 (  0.140826)
Proxy method_missing   0.130000   0.000000   0.130000 (  0.158820)
extend                 0.570000   0.040000   0.610000 (  0.602327)
extend using quux      0.440000   0.020000   0.460000 (  0.474000)
----------------------------------------------- total: 1.330000sec

                           user     system      total        real
Proxy                  0.140000   0.010000   0.150000 (  0.150230)
Proxy method_missing   0.130000   0.010000   0.140000 (  0.134410)
extend                 0.540000   0.020000   0.560000 (  0.582140)
extend using quux      0.440000   0.020000   0.460000 (  0.460955)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment