Skip to content

Instantly share code, notes, and snippets.

@mtkd
Created May 10, 2014 16:38
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 mtkd/9bcfe207d90829df6078 to your computer and use it in GitHub Desktop.
Save mtkd/9bcfe207d90829df6078 to your computer and use it in GitHub Desktop.
super vs super if defined?(super)
# 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