Skip to content

Instantly share code, notes, and snippets.

@tinogomes
Created April 30, 2010 18:17
Show Gist options
  • Save tinogomes/385573 to your computer and use it in GitHub Desktop.
Save tinogomes/385573 to your computer and use it in GitHub Desktop.
Benchmark: !!(value) or (value == expected)
# Benchmark: !!(value) or (value == expected)
require 'benchmark'
n = 5000000
value = :value
Benchmark.bm do |x|
x.report { p( !!(1 < 0 || 1 > 0) ) }
x.report { p( (1 < 0 || 1 > 0) == true ) }
x.report { p( !!(nil || value) ) }
x.report { p( (nil || value) == :value ) }
x.report { p( !!(nil) ) }
x.report { p( (nil).nil? == false ) }
end
################################################
user system total real
true
0.000000 0.000000 0.000000 ( 0.000025)
true
0.000000 0.000000 0.000000 ( 0.000019)
true
0.000000 0.000000 0.000000 ( 0.000017)
true
0.000000 0.000000 0.000000 ( 0.000026)
false
0.000000 0.000000 0.000000 ( 0.000026)
false
0.000000 0.000000 0.000000 ( 0.000024)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment