Created
May 10, 2014 16:38
-
-
Save mtkd/9bcfe207d90829df6078 to your computer and use it in GitHub Desktop.
super vs super if defined?(super)
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
# Test to see how much time checking super defined costs (approx 10%) | |
require 'benchmark' | |
class Base | |
def meth; end | |
end | |
class WithSuperCheck < Base | |
def meth; super if defined?(super); end | |
end | |
class WithoutSuperCheck < Base | |
def meth; super; end | |
end | |
Benchmark.bm do |x| | |
x.report { 10000000.times do; WithSuperCheck.new.meth ; end; } | |
x.report { 10000000.times do; WithoutSuperCheck.new.meth ; end; } | |
end | |
# user system total real | |
# 2.440000 0.000000 2.440000 ( 2.440145) | |
# 2.180000 0.000000 2.180000 ( 2.181029) | |
# | |
# user system total real | |
# 2.420000 0.000000 2.420000 ( 2.427584) | |
# 2.180000 0.000000 2.180000 ( 2.177681) | |
# | |
# user system total real | |
# 2.430000 0.000000 2.430000 ( 2.432584) | |
# 2.160000 0.000000 2.160000 ( 2.168029) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment