Created
February 25, 2015 00:51
-
-
Save avdi/af5d7d84db9173a22f19 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 "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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is why I love benchmark-ips so much – does the right amount of running, shows standard deviation and a comparison. Three subsequent runs on MRI 2.2.0-p0 and i7-3517U: