Skip to content

Instantly share code, notes, and snippets.

@davidcelis
Created December 28, 2016 02:51
Show Gist options
  • Save davidcelis/066e1f83dc914e9086967eb81b851dda to your computer and use it in GitHub Desktop.
Save davidcelis/066e1f83dc914e9086967eb81b851dda to your computer and use it in GitHub Desktop.
user system total real
Forwardable 0.170000 0.000000 0.170000 ( 0.167711)
Send 0.110000 0.000000 0.110000 ( 0.106545)
Simple call 0.070000 0.000000 0.070000 ( 0.070068)
require 'benchmark'
require 'forwardable'
class TestForwardable
extend Forwardable
def_delegators :@array, :size
def initialize
@array = (1..100).to_a
end
end
class TestMethod
def initialize
@array = (1..100).to_a
end
def size
@array.size
end
end
class TestSend
def initialize
@array = (1..100).to_a
end
def size
@array.send(:size)
end
end
forwardable = TestForwardable.new
sender = TestSend.new
method = TestMethod.new
Benchmark.bm do |x|
x.report("Forwardable") { 1_000_000.times { forwardable.size } }
x.report("Send") { 1_000_000.times { sender.size } }
x.report("Simple call") { 1_000_000.times { method.size } }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment