Skip to content

Instantly share code, notes, and snippets.

@equivalent
Last active October 25, 2019 06:07
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 equivalent/eaf34a7e5be64f21a979f5681ee21f13 to your computer and use it in GitHub Desktop.
Save equivalent/eaf34a7e5be64f21a979f5681ee21f13 to your computer and use it in GitHub Desktop.
Benchmark for definded? vs nil? discussion
require 'benchmark'
n = 5000000
@variable = "whatever value"
Benchmark.bmbm do |x|
x.report { @variable.nil? }
x.report { defined?(@variable) }
end
# Rehearsal ------------------------------------
# 0.000015 0.000009 0.000024 ( 0.000014)
# 0.000010 0.000006 0.000016 ( 0.000011)
# --------------------------- total: 0.000040sec
#
# user system total real
# 0.000007 0.000003 0.000010 ( 0.000004)
# 0.000010 0.000000 0.000010 ( 0.000005)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment