Skip to content

Instantly share code, notes, and snippets.

@jonleighton
Created January 19, 2012 14:09
Show Gist options
  • Save jonleighton/1640199 to your computer and use it in GitHub Desktop.
Save jonleighton/1640199 to your computer and use it in GitHub Desktop.
require 'benchmark'
class Foo1
def foo
"bar"
end
end
class Foo2
module Other
def foo
"bar"
end
end
include Other
def foo
super
end
end
class Foo3
def foo
"bar"
end
alias old_foo foo
undef_method :foo
def foo
old_foo
end
end
n = 10_000_000
foo1 = Foo1.new
foo2 = Foo2.new
foo3 = Foo3.new
Benchmark.bm(20) do |r|
r.report('normal') do
n.times { foo1.foo }
end
r.report('with super') do
n.times { foo2.foo }
end
r.report('with delegation') do
n.times { foo3.foo }
end
end
user system total real
normal 1.939000 0.000000 1.939000 ( 1.939000)
with super 2.309000 0.000000 2.309000 ( 2.309000)
with delegation 1.439000 0.000000 1.439000 ( 1.439000)
user system total real
normal 3.250000 0.000000 3.250000 ( 3.278874)
with super 4.420000 0.000000 4.420000 ( 4.454511)
with delegation 3.970000 0.000000 3.970000 ( 4.001511)
user system total real
normal 1.704741 0.000000 1.704741 ( 2.116214)
with super 5.235204 0.001000 5.236204 ( 5.369124)
with delegation 1.488773 0.000000 1.488773 ( 1.508955)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment