Skip to content

Instantly share code, notes, and snippets.

@lukaszkorecki
Forked from plcstevens/Results
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukaszkorecki/9d242aaa188b03659b68 to your computer and use it in GitHub Desktop.
Save lukaszkorecki/9d242aaa188b03659b68 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 } }
x.report('smart ass') { 100.times { float_array.min rescue nil } }
end
Rehearsal ---------------------------------------------
any? 1.270000 0.010000 1.280000 ( 1.285305)
all? 0.870000 0.000000 0.870000 ( 0.880658)
min block 1.270000 0.010000 1.280000 ( 1.284267)
smart ass 0.670000 0.010000 0.680000 ( 0.689223)
------------------------------------ total: 4.110000sec
user system total real
any? 1.240000 0.010000 1.250000 ( 1.258006)
all? 0.880000 0.010000 0.890000 ( 0.890891)
min block 1.240000 0.010000 1.250000 ( 1.262233)
smart ass 0.670000 0.000000 0.670000 ( 0.673408)
Rehearsal ---------------------------------------------
any? 0.000000 0.000000 0.000000 ( 0.000606)
all? 0.000000 0.000000 0.000000 ( 0.000224)
min block 1.240000 0.010000 1.250000 ( 1.254697)
smart ass 0.000000 0.000000 0.000000 ( 0.001102)
------------------------------------ total: 1.250000sec
user system total real
any? 0.000000 0.000000 0.000000 ( 0.000510)
all? 0.000000 0.000000 0.000000 ( 0.000295)
min block 1.240000 0.010000 1.250000 ( 1.258530)
smart ass 0.000000 0.000000 0.000000 ( 0.001134)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment