Skip to content

Instantly share code, notes, and snippets.

@sbeckeriv
Forked from avdi/delegator_bench.rb
Last active August 29, 2015 14:16
Show Gist options
  • Save sbeckeriv/fa0b8ada864eb66469be to your computer and use it in GitHub Desktop.
Save sbeckeriv/fa0b8ada864eb66469be to your computer and use it in GitHub Desktop.
require "delegate"
class BankAccount
def number
123456
end
end
class SimpleDelegatorBankAccount < SimpleDelegator
end
class DelegateClassBankAccount < DelegateClass(BankAccount)
end
class ManualDelegatorBankAccount
def initialize(target)
@target = target
end
def number
@target.number
end
end
account = BankAccount.new
sdba = SimpleDelegatorBankAccount.new(account)
dcba = DelegateClassBankAccount.new(account)
mdba = ManualDelegatorBankAccount.new(account)
require "benchmark"
puts RUBY_VERSION
n = 1_000_000
Benchmark.bmbm(20) do |x|
x.report("direct call") do
n.times do
_ = account.number
end
end
x.report("manual delegation") do
n.times do
_ = mdba.number
end
end
x.report("SimpleDelegator") do
n.times do
_ = sdba.number
end
end
x.report("DelegateClass") do
n.times do
_ = dcba.number
end
end
end
# >> 2.2.0
# >> Rehearsal --------------------------------------------------------
# >> direct call 2.210000 0.020000 2.230000 ( 2.332280)
# >> manual delegation 3.410000 0.070000 3.480000 ( 3.500194)
# >> SimpleDelegator 2.850000 0.060000 2.910000 ( 2.920977)
# >> DelegateClass 3.370000 0.020000 3.390000 ( 3.411052)
# >> ---------------------------------------------- total: 12.010000sec
# >>
# >> user system total real
# >> direct call 1.800000 0.030000 1.830000 ( 1.834531)
# >> manual delegation 2.830000 0.130000 2.960000 ( 2.958950)
# >> SimpleDelegator 2.960000 0.040000 3.000000 ( 3.038120)
# >> DelegateClass 2.390000 0.030000 2.420000 ( 2.878221)
=begin #beckers
ruby bm2.rb
2.1.3
Rehearsal --------------------------------------------------------
direct call 0.060000 0.000000 0.060000 ( 0.060608)
manual delegation 0.080000 0.000000 0.080000 ( 0.079547)
SimpleDelegator 0.320000 0.000000 0.320000 ( 0.320238)
DelegateClass 0.270000 0.000000 0.270000 ( 0.275023)
----------------------------------------------- total: 0.730000sec
user system total real
direct call 0.070000 0.000000 0.070000 ( 0.063817)
manual delegation 0.080000 0.000000 0.080000 ( 0.086517)
SimpleDelegator 0.330000 0.000000 0.330000 ( 0.332198)
DelegateClass 0.290000 0.000000 0.290000 ( 0.295056)
/usr/local/Cellar/ruby/2.2.0/bin/ruby bm2.rb
ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin14]
Rehearsal --------------------------------------------------------
direct call 0.070000 0.000000 0.070000 ( 0.067561)
manual delegation 0.090000 0.000000 0.090000 ( 0.090520)
SimpleDelegator 0.370000 0.000000 0.370000 ( 0.369554)
DelegateClass 0.290000 0.000000 0.290000 ( 0.294461)
----------------------------------------------- total: 0.820000sec
user system total real
direct call 0.070000 0.000000 0.070000 ( 0.065939)
manual delegation 0.090000 0.000000 0.090000 ( 0.094444)
SimpleDelegator 0.370000 0.000000 0.370000 ( 0.364165)
DelegateClass 0.300000 0.000000 0.300000 ( 0.302042)
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment