Last active
June 28, 2016 16:04
Revisions
-
mjgiarlo revised this gist
Nov 27, 2013 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal 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('map') { enum.map { |v| process(v) if i_want_this?(v) }.reject { |v| !v } } end -
mjgiarlo revised this gist
Nov 27, 2013 . 1 changed file with 10 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal 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.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.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) -
mjgiarlo created this gist
Nov 27, 2013 .There are no files selected for viewing
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 charactersOriginal 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)