Skip to content

Instantly share code, notes, and snippets.

@sikachu
Created December 5, 2011 01:47
Show Gist options
  • Save sikachu/1431961 to your computer and use it in GitHub Desktop.
Save sikachu/1431961 to your computer and use it in GitHub Desktop.
Compare between not define a method in subclass and define a method in subclass and call #super
require 'benchmark'
class A0
def foo
true
end
end
(1..1000).each do |n|
eval <<-RUBY
class A#{n} < A#{n-1}
end
RUBY
end
class B0
def foo
true
end
end
(1..1000).each do |n|
eval <<-RUBY
class B#{n} < B#{n-1}
def foo
super
end
end
RUBY
end
Benchmark.bmbm do |x|
x.report("implicit") { A1000.new.foo }
x.report("explicit") { B1000.new.foo }
end
[~] ruby benchmark.rb
Rehearsal --------------------------------------------
implicit 0.010000 0.000000 0.010000 ( 0.000501)
explicit 0.000000 0.000000 0.000000 ( 0.008302)
----------------------------------- total: 0.010000sec
user system total real
implicit 0.000000 0.000000 0.000000 ( 0.000139)
explicit 0.010000 0.000000 0.010000 ( 0.007057)
[~]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment