Skip to content

Instantly share code, notes, and snippets.

@plcstevens
Created January 6, 2015 11:06
Show Gist options
  • Save plcstevens/f6ee4fed5e29b76856e0 to your computer and use it in GitHub Desktop.
Save plcstevens/f6ee4fed5e29b76856e0 to your computer and use it in GitHub Desktop.

Revisions

  1. plcstevens created this gist Jan 6, 2015.
    10 changes: 10 additions & 0 deletions Results
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    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)
    11 changes: 11 additions & 0 deletions bench.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    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