Created
July 9, 2012 12:59
-
-
Save joel/3076423 to your computer and use it in GitHub Desktop.
Comparaison blank? present? Nil ?
This file contains hidden or 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 'rubygems' | |
# Set up gems listed in the Gemfile. | |
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('Gemfile', __FILE__) | |
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE']) | |
require 'rails/all' | |
N = 10000000 | |
EMPTY_STRING = '' | |
Benchmark.bmbm do |x| | |
x.report('empty') { N.times { | |
EMPTY_STRING.empty? | |
} } | |
x.report('== empty') { N.times { | |
EMPTY_STRING == '' | |
} } | |
x.report('== nil') { N.times { | |
EMPTY_STRING == nil | |
} } | |
x.report('nil') { N.times { | |
EMPTY_STRING.nil? | |
} } | |
x.report('== nil and empty') { N.times { | |
EMPTY_STRING == nil && EMPTY_STRING != '' | |
} } | |
x.report('== presence and empty') { N.times { | |
EMPTY_STRING && EMPTY_STRING != '' | |
} } | |
# BEST | |
x.report('nil and empty') { N.times { | |
EMPTY_STRING.nil? && !EMPTY_STRING.empty? | |
} } | |
x.report('presence and empty') { N.times { | |
EMPTY_STRING && !EMPTY_STRING.empty? | |
} } | |
x.report('blank') { N.times { | |
EMPTY_STRING.blank? | |
} } | |
end | |
# https://gist.github.com/31df105283e577987c00 | |
# user system total real | |
# empty 1.190000 0.000000 1.190000 ( 1.208954) | |
# == empty 2.950000 0.010000 2.960000 ( 3.343867) | |
# == nil 1.940000 0.010000 1.950000 ( 1.996039) | |
# nil 1.170000 0.000000 1.170000 ( 1.222273) | |
# == nil and empty 1.940000 0.010000 1.950000 ( 2.068318) | |
# == presence and empty 2.940000 0.010000 2.950000 ( 3.014012) | |
# nil and empty 1.220000 0.000000 1.220000 ( 1.480397) | |
# presence and empty 1.360000 0.000000 1.360000 ( 1.383167) | |
# blank 9.290000 0.030000 9.320000 ( 9.797732) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment