Last active
August 29, 2015 14:14
-
-
Save benweint/67c5d596df32b4313165 to your computer and use it in GitHub Desktop.
forwardable
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
#!/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
commented
Feb 4, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment