benchmark for article Ruby Memoization: ||= vs. Defined? Syntax
Last active
October 25, 2019 06:07
-
-
Save equivalent/eaf34a7e5be64f21a979f5681ee21f13 to your computer and use it in GitHub Desktop.
Benchmark for definded? vs nil? discussion
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
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