Skip to content

Instantly share code, notes, and snippets.

@quad
Last active April 3, 2017 09:59
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 quad/497dab7759cf0554db1b to your computer and use it in GitHub Desktop.
Save quad/497dab7759cf0554db1b to your computer and use it in GitHub Desktop.
Because Rubyists knows the value of everything, but the cost of nothing.
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