Last active
April 3, 2017 09:59
-
-
Save quad/497dab7759cf0554db1b to your computer and use it in GitHub Desktop.
Because Rubyists knows the value of everything, but the cost of nothing.
This file contains 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 'benchmark' | |
require 'delegate' | |
class Direct | |
def method | |
'direct' | |
end | |
def base | |
'base' | |
end | |
end | |
module Role | |
def method | |
'role' | |
end | |
end | |
class Delegated < SimpleDelegator | |
def method | |
'delegator' | |
end | |
end | |
puts 'Injected method hit' | |
def heaps &block | |
100_000.times &block | |
end | |
Benchmark.bmbm do |x| | |
x.report('direct') { heaps { Direct.new.method } } | |
x.report('role') do | |
heaps do | |
d = Direct.new | |
d.extend Role | |
d.method | |
end | |
end | |
x.report('delegator') do | |
heaps do | |
d = Delegated.new Direct.new | |
d.method | |
end | |
end | |
end | |
puts | |
puts 'Base method hit' | |
Benchmark.bmbm do |x| | |
x.report('direct') { heaps { Direct.new.base } } | |
x.report('role') do | |
heaps do | |
d = Direct.new | |
d.extend Role | |
d.base | |
end | |
end | |
x.report('delegator') do | |
heaps do | |
d = Delegated.new Direct.new | |
d.base | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment