Last active
December 26, 2015 13:39
-
-
Save ihower/7160400 to your computer and use it in GitHub Desktop.
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 'casting' | |
require 'benchmark' | |
require 'delegate' | |
module Greeter | |
def hello | |
%{hello from #{self}} | |
end | |
end | |
class Person | |
end | |
class InheritedPerson < Person | |
def hello | |
%{hello from #{self}} | |
end | |
end | |
class MixinPerson | |
include Greeter | |
end | |
class CastingPerson | |
include Casting::Client | |
end | |
class CastingAllPerson | |
include Casting::Client | |
delegate_missing_methods | |
end | |
class DelegatorPerson < SimpleDelegator | |
def hello | |
%{hello from #{self}} | |
end | |
end | |
class WrapperPerson | |
def initialize(person) | |
@person = person | |
end | |
def hello | |
%{hello from #{@person}} | |
end | |
end | |
n = 100000 | |
Benchmark.bm do |x| | |
x.report("inherited") { n.times { | |
person0 = InheritedPerson.new | |
person0.hello | |
}} | |
x.report("mixin") { n.times { | |
person1 = MixinPerson.new | |
person1.hello | |
}} | |
x.report("my wrapper") { n.times { | |
person2 = WrapperPerson.new(Person.new) | |
person2.hello | |
}} | |
x.report("simple delegator") { n.times { | |
person3 = DelegatorPerson.new(Person.new) | |
person3.hello | |
}} | |
x.report("dynamic extend") { n.times { | |
person4 = Person.new | |
person4.extend(Greeter) | |
person4.hello | |
}} | |
#x.report("casting") { n.times { | |
# CastingPerson.new.delegate(:hello, Greeter) | |
#}} | |
#x.report("casting all") { n.times { | |
# person6 = CastingAllPerson.new | |
# person6.cast_as(Greeter) | |
# person6.hello | |
#}} | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment