Skip to content

Instantly share code, notes, and snippets.

@akshay-vishnoi
Last active December 31, 2015 17:49

Revisions

  1. akshay-vishnoi revised this gist Dec 18, 2013. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion many_performance.rb
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,6 @@ def old_many?(enum)
    end

    def new_many?(enum)
    cnt = 0
    enum.count > 1
    end

  2. akshay-vishnoi revised this gist Dec 18, 2013. No changes.
  3. akshay-vishnoi renamed this gist Dec 18, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. akshay-vishnoi revised this gist Dec 18, 2013. No changes.
  5. akshay-vishnoi created this gist Dec 18, 2013.
    31 changes: 31 additions & 0 deletions PerformanceImprovement
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    require 'benchmark/ips'

    def old_many?(enum)
    cnt = 0
    enum.any? { (cnt += 1) > 1 }
    end

    def new_many?(enum)
    cnt = 0
    enum.count > 1
    end

    enum = (1..1000).to_a

    Benchmark.ips do |b|
    b.report('old') do
    old_many? enum
    end

    b.report('new') do
    new_many? enum
    end
    end

    # Output
    # Calculating -------------------------------------
    # old 57932 i/100ms
    # new 93881 i/100ms
    # -------------------------------------------------
    # old 1312923.5 (±3.1%) i/s - 6604248 in 5.035705s
    # new 4991185.8 (±3.2%) i/s - 24972346 in 5.009269s