Skip to content

Instantly share code, notes, and snippets.

@ihower
Last active December 26, 2015 13:39
Show Gist options
  • Select an option

  • Save ihower/7160400 to your computer and use it in GitHub Desktop.

Select an option

Save ihower/7160400 to your computer and use it in GitHub Desktop.

Revisions

  1. ihower revised this gist Oct 25, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@ def hello
    end
    end

    class FastPerson
    class MixinPerson
    include Greeter
    end

    @@ -53,7 +53,7 @@ def hello
    person0.hello
    }}
    x.report("mixin") { n.times {
    person1 = FastPerson.new
    person1 = MixinPerson.new
    person1.hello
    }}
    x.report("my wrapper") { n.times {
  2. ihower created this gist Oct 25, 2013.
    82 changes: 82 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,82 @@
    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 FastPerson
    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 = FastPerson.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