Skip to content

Instantly share code, notes, and snippets.

@rhearnorth
Last active August 29, 2015 14:17
Show Gist options
  • Save rhearnorth/3bee65e73e73297c3f33 to your computer and use it in GitHub Desktop.
Save rhearnorth/3bee65e73e73297c3f33 to your computer and use it in GitHub Desktop.
Benchmarking any? all? none?
require 'benchmark'
cases = [
[true, true, true, true, false, true, true, true, true, false], # Mix
[true, true, true, true, true, true, true, true, true, true], # All true
[false, false, false, false, false, false, false, false, false, false] # All false
]
Benchmark.bmbm do |x|
cases.each do |test_case|
x.report("any?") do
100.times { test_case.any? }
end
x.report("all?") do
100.times { test_case.all? }
end
x.report("none?") do
100.times { test_case.none? }
end
end
end
=begin
Rehearsal -----------------------------------------
any? 0.000000 0.000000 0.000000 ( 0.000063)
all? 0.000000 0.000000 0.000000 ( 0.000076)
none? 0.000000 0.000000 0.000000 ( 0.000046) # fastest
any? 0.000000 0.000000 0.000000 ( 0.000041) # fastest
all? 0.000000 0.000000 0.000000 ( 0.000072)
none? 0.000000 0.000000 0.000000 ( 0.000062)
any? 0.000000 0.000000 0.000000 ( 0.000060)
all? 0.000000 0.000000 0.000000 ( 0.000050) # fastest
none? 0.000000 0.000000 0.000000 ( 0.000053)
-------------------------------- total: 0.000000sec
user system total real
any? 0.000000 0.000000 0.000000 ( 0.000033)
all? 0.000000 0.000000 0.000000 ( 0.000037)
none? 0.000000 0.000000 0.000000 ( 0.000028) # fastest
any? 0.000000 0.000000 0.000000 ( 0.000028) # fastest
all? 0.000000 0.000000 0.000000 ( 0.000047)
none? 0.000000 0.000000 0.000000 ( 0.000047)
any? 0.000000 0.000000 0.000000 ( 0.000094)
all? 0.000000 0.000000 0.000000 ( 0.000042) # fastest
none? 0.000000 0.000000 0.000000 ( 0.000064)
=end
@rhearnorth
Copy link
Author

Conclusion: better to use none? or any? rather than all?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment