Skip to content

Instantly share code, notes, and snippets.

@mjgiarlo
Last active June 28, 2016 16:04

Revisions

  1. mjgiarlo revised this gist Nov 27, 2013. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions benchmark_dead_horse.rb
    Original file line number Diff line number Diff line change
    @@ -13,10 +13,10 @@ def i_want_this?(v)
    end

    Benchmark.bmbm do |x|
    x.report('inj'){ enum.inject([]) { |arr, v| arr << process(v) if i_want_this?(v); arr } }
    x.report('tap'){ [].tap { |arr| enum.each { |v| arr << process(v) if i_want_this?(v) } } }
    x.report('ewo'){ enum.each_with_object([]) { |v, arr| arr << process(v) if i_want_this?(v) } }
    x.report('sel') { enum.select{|v| i_want_this?(v) }.map { |v| process(v) } }
    x.report('inj') { enum.inject([]) { |arr, v| arr << process(v) if i_want_this?(v); arr } }
    x.report('tap') { [].tap { |arr| enum.each { |v| arr << process(v) if i_want_this?(v) } } }
    x.report('ewo') { enum.each_with_object([]) { |v, arr| arr << process(v) if i_want_this?(v) } }
    x.report('sel') { enum.select {|v| i_want_this?(v) }.map { |v| process(v) } }
    x.report('map') { enum.map { |v| process(v) if i_want_this?(v) }.reject { |v| !v } }
    end

  2. mjgiarlo revised this gist Nov 27, 2013. 1 changed file with 10 additions and 4 deletions.
    14 changes: 10 additions & 4 deletions benchmark_dead_horse.rb
    Original file line number Diff line number Diff line change
    @@ -16,16 +16,22 @@ def i_want_this?(v)
    x.report('inj'){ enum.inject([]) { |arr, v| arr << process(v) if i_want_this?(v); arr } }
    x.report('tap'){ [].tap { |arr| enum.each { |v| arr << process(v) if i_want_this?(v) } } }
    x.report('ewo'){ enum.each_with_object([]) { |v, arr| arr << process(v) if i_want_this?(v) } }
    x.report('sel') { enum.select{|v| i_want_this?(v) }.map { |v| process(v) } }
    x.report('map') { enum.map { |v| process(v) if i_want_this?(v) }.reject { |v| !v } }
    end


    # Rehearsal ---------------------------------------
    # inj 0.000000 0.000000 0.000000 ( 0.000027)
    # tap 0.000000 0.000000 0.000000 ( 0.000017)
    # ewo 0.000000 0.000000 0.000000 ( 0.000024)
    # ewo 0.000000 0.000000 0.000000 ( 0.000023)
    # sel 0.000000 0.000000 0.000000 ( 0.000016)
    # map 0.000000 0.000000 0.000000 ( 0.000021)
    # ------------------------------ total: 0.000000sec
    #
    # user system total real
    # inj 0.000000 0.000000 0.000000 ( 0.000020)
    # tap 0.000000 0.000000 0.000000 ( 0.000022)
    # ewo 0.000000 0.000000 0.000000 ( 0.000024)
    # inj 0.000000 0.000000 0.000000 ( 0.000025)
    # tap 0.000000 0.000000 0.000000 ( 0.000024)
    # ewo 0.000000 0.000000 0.000000 ( 0.000026)
    # sel 0.000000 0.000000 0.000000 ( 0.000023)
    # map 0.000000 0.000000 0.000000 ( 0.000024)
  3. mjgiarlo created this gist Nov 27, 2013.
    31 changes: 31 additions & 0 deletions benchmark_dead_horse.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    require 'benchmark'

    N = 1000000
    nums = N.times.map{ rand(N) }
    enum = [1, 2, 'string', {}, [], false, true, nil]

    def process(v)
    v
    end

    def i_want_this?(v)
    !v.nil?
    end

    Benchmark.bmbm do |x|
    x.report('inj'){ enum.inject([]) { |arr, v| arr << process(v) if i_want_this?(v); arr } }
    x.report('tap'){ [].tap { |arr| enum.each { |v| arr << process(v) if i_want_this?(v) } } }
    x.report('ewo'){ enum.each_with_object([]) { |v, arr| arr << process(v) if i_want_this?(v) } }
    end


    # Rehearsal ---------------------------------------
    # inj 0.000000 0.000000 0.000000 ( 0.000027)
    # tap 0.000000 0.000000 0.000000 ( 0.000017)
    # ewo 0.000000 0.000000 0.000000 ( 0.000024)
    # ------------------------------ total: 0.000000sec
    #
    # user system total real
    # inj 0.000000 0.000000 0.000000 ( 0.000020)
    # tap 0.000000 0.000000 0.000000 ( 0.000022)
    # ewo 0.000000 0.000000 0.000000 ( 0.000024)