Created
January 6, 2015 11:06
-
-
Save plcstevens/f6ee4fed5e29b76856e0 to your computer and use it in GitHub Desktop.
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" | |
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 |
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
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