Skip to content

Instantly share code, notes, and snippets.

@ferrous26
Created February 5, 2011 22:23
Show Gist options
  • Save ferrous26/812863 to your computer and use it in GitHub Desktop.
Save ferrous26/812863 to your computer and use it in GitHub Desktop.
Benchmarking if not vs unless
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
@ferrous26
Copy link
Author

With CRuby 1.9.2 on my laptop

Rehearsal ---------------------------------------------------------------------------------
if not                                          1.370000   0.010000   1.380000 (  1.384264)
unless                                          1.300000   0.000000   1.300000 (  1.326188)
if not without explicit nil check               1.000000   0.000000   1.000000 (  1.024153)
unless without explicit nil check               0.880000   0.000000   0.880000 (  0.907189)
if not without explicit nil check on one line   1.000000   0.000000   1.000000 (  1.020711)
unless without explicit nil check on one line   0.880000   0.010000   0.890000 (  0.903711)
------------------------------------------------------------------------ total: 6.450000sec

                                                    user     system      total        real
if not                                          1.360000   0.000000   1.360000 (  1.390325)
unless                                          1.310000   0.000000   1.310000 (  1.362429)
if not without explicit nil check               0.990000   0.010000   1.000000 (  1.049561)
unless without explicit nil check               0.890000   0.000000   0.890000 (  0.921260)
if not without explicit nil check on one line   0.990000   0.000000   0.990000 (  1.018209)
unless without explicit nil check on one line   0.880000   0.000000   0.880000 (  0.931479)

@ferrous26
Copy link
Author

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