Created
May 3, 2018 14:01
-
-
Save rolentle/98e9385d0d777ea05ae30e4e8136ef91 to your computer and use it in GitHub Desktop.
any vs length > 0
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 'benchmark' | |
a = [] | |
iterations = 1_000_000 | |
puts "for empty array" | |
Benchmark.bm do |x| | |
x.report("length") { iterations.times do; a.length > 0; end } | |
x.report("any") { iterations.times do; a.any?; end } | |
end | |
# user system total real | |
#length 0.030000 0.000000 0.030000 ( 0.038053) | |
#any 0.040000 0.000000 0.040000 ( 0.056048) | |
puts "for full array" | |
b = (1..100).to_a | |
Benchmark.bm do |x| | |
x.report("length") { iterations.times do; b.length > 0; end } | |
x.report("any") { iterations.times do; b.any?; end } | |
end | |
# user system total real | |
#length 0.040000 0.000000 0.040000 ( 0.039192) | |
#any 0.050000 0.000000 0.050000 ( 0.056181) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment