Skip to content

Instantly share code, notes, and snippets.

@plcstevens
Created January 6, 2015 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save plcstevens/f6ee4fed5e29b76856e0 to your computer and use it in GitHub Desktop.
Save plcstevens/f6ee4fed5e29b76856e0 to your computer and use it in GitHub Desktop.
require "benchmark"
float_array = 100000.times.map { rand }
1000.times { float_array << nil }
float_array.shuffle!
Benchmark.bmbm do |x|
x.report("any?") { 100.times { float_array.any?(&:nil?) ? nil : float_array.min } }
x.report("all?") { 100.times { float_array.all? ? float_array.min : nil } }
x.report("min block") { 100.times { minimum = float_array.min { |a, b| a && b ? a <=> b : -1 }; minimum == -1 ? nil : minimum } }
end
Rehearsal ---------------------------------------------
any? 0.000000 0.000000 0.000000 ( 0.000885)
all? 0.000000 0.000000 0.000000 ( 0.000279)
min block 0.960000 0.000000 0.960000 ( 0.958517)
------------------------------------ total: 0.960000sec
user system total real
any? 0.000000 0.000000 0.000000 ( 0.000886)
all? 0.000000 0.000000 0.000000 ( 0.000297)
min block 0.970000 0.000000 0.970000 ( 0.974703)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment