Created
February 5, 2011 22:23
-
-
Save ferrous26/812863 to your computer and use it in GitHub Desktop.
Benchmarking if not vs unless
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 = 10_000_000 | |
Benchmark.bmbm do |test| | |
test.report 'if not' do | |
n.times do | |
if not 0.nil? | |
0 | |
end | |
end | |
end | |
test.report 'unless' do | |
n.times do | |
unless 0.nil? | |
0 | |
end | |
end | |
end | |
test.report 'if not without explicit nil check' do | |
n.times do | |
if not 0 | |
0 | |
end | |
end | |
end | |
test.report 'unless without explicit nil check' do | |
n.times do | |
unless 0 | |
0 | |
end | |
end | |
end | |
test.report 'if not without explicit nil check on one line' do | |
n.times do | |
0 if not 0 | |
end | |
end | |
test.report 'unless without explicit nil check on one line' do | |
n.times do | |
0 unless 0 | |
end | |
end | |
end |
With MacRuby 0.9-nightly on my laptop
Rehearsal ---------------------------------------------------------------------------------
if not 0.990000 0.010000 1.000000 ( 1.007533)
unless 0.960000 0.000000 0.960000 ( 0.989678)
if not without explicit nil check 0.430000 0.000000 0.430000 ( 0.444077)
unless without explicit nil check 0.460000 0.000000 0.460000 ( 0.466261)
if not without explicit nil check on one line 0.430000 0.000000 0.430000 ( 0.447271)
unless without explicit nil check on one line 0.440000 0.000000 0.440000 ( 0.446631)
------------------------------------------------------------------------ total: 3.720000sec
user system total real
if not 0.970000 0.000000 0.970000 ( 0.994858)
unless 0.960000 0.010000 0.970000 ( 0.969920)
if not without explicit nil check 0.440000 0.000000 0.440000 ( 0.441751)
unless without explicit nil check 0.430000 0.000000 0.430000 ( 0.441213)
if not without explicit nil check on one line 0.430000 0.000000 0.430000 ( 0.446860)
unless without explicit nil check on one line 0.430000 0.000000 0.430000 ( 0.451033)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With CRuby 1.9.2 on my laptop