Skip to content

Instantly share code, notes, and snippets.

@benweint
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benweint/67c5d596df32b4313165 to your computer and use it in GitHub Desktop.
Save benweint/67c5d596df32b4313165 to your computer and use it in GitHub Desktop.
forwardable
#!/usr/bin/env ruby
require 'benchmark'
require 'forwardable'
class Horse
def bark(n); end
end
class ForwardedHorse
extend Forwardable
def_delegators :@horse, :bark
def initialize
@horse = Horse.new
end
end
class ExplicitlyForwardedHorse
def initialize
@horse = Horse.new
end
def bark(x)
@horse.bark(x)
end
end
N = 10000000
Benchmark.bmbm do |x|
%w[ForwardedHorse ExplicitlyForwardedHorse].each do |klass|
horse = Object.const_get(klass).new
x.report("#{klass}#bark") do
N.times do
horse.bark(1)
end
end
end
end
@davidcelis
Copy link

  %w[ForwardedHorse ExplicitlyForwardedHorse].each do |klass|
    horse = Object.const_get(klass).new

    x.report("#{klass}#bark") do
      N.times do
        horse.bark(1)
      end
    end
  end

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