Skip to content

Instantly share code, notes, and snippets.

@franckverrot
Forked from jgaskins/extend_vs_proxy.rb
Last active August 29, 2015 14:14
Show Gist options
  • Save franckverrot/574e8948cf1679a317fa to your computer and use it in GitHub Desktop.
Save franckverrot/574e8948cf1679a317fa to your computer and use it in GitHub Desktop.
require 'benchmark/ips'
class Foo
def bar
# "Foo#bar"
end
end
module Bar
def bar
# "Bar#bar"
end
end
class FooWrapper
attr_reader :obj
def initialize(obj)
@obj = obj
end
def bar
# "FooWrapper#bar"
end
def method_missing *args, &block
obj.public_send *args, &block
end
end
Benchmark.ips do |x|
x.report "plain" do
Foo.new.bar
end
x.report "extend" do
Foo.new.extend(Bar).bar
end
x.report "proxy" do
FooWrapper.new(Foo.new).bar
end
Foo.extend Bar
x.report "Foo extended once" do
Foo.new.bar
end
end
# Calculating -------------------------------------
# plain 135.946k i/100ms
# extend 34.635k i/100ms
# proxy 112.357k i/100ms
# -------------------------------------------------
# plain 4.939M (± 5.0%) i/s - 24.742M
# extend 438.022k (± 4.1%) i/s - 2.217M
# proxy 2.676M (± 4.1%) i/s - 13.370M
Calculating -------------------------------------
plain 56.037k i/100ms
extend 13.941k i/100ms
proxy 46.194k i/100ms
Foo extended once 56.594k i/100ms
-------------------------------------------------
plain 2.185M (±13.2%) i/s - 10.479M
extend 172.919k (± 9.5%) i/s - 864.342k
proxy 1.242M (±10.4%) i/s - 6.144M
Foo extended once 2.169M (± 9.3%) i/s - 10.753M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment