Skip to content

Instantly share code, notes, and snippets.

@mjgiarlo
Last active June 28, 2016 16:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mjgiarlo/7680957 to your computer and use it in GitHub Desktop.
Save mjgiarlo/7680957 to your computer and use it in GitHub Desktop.
Ruby inject vs. tap vs. each_with_object. Inspired by http://phrogz.net/tap-vs-each_with_object
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) } }
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)
@adjam
Copy link

adjam commented Nov 27, 2013

For no raisin:

$ ruby -v
ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-darwin12.5.0]
$ ruby deadhorse.rb 
Rehearsal ---------------------------------------
inj   0.000000   0.000000   0.000000 (  0.000015)
tap   0.000000   0.000000   0.000000 (  0.000008)
ewo   0.000000   0.000000   0.000000 (  0.000011)
sel   0.000000   0.000000   0.000000 (  0.000008)
map   0.000000   0.000000   0.000000 (  0.000015)
------------------------------ total: 0.000000sec

          user     system      total        real
inj   0.000000   0.000000   0.000000 (  0.000014)
tap   0.000000   0.000000   0.000000 (  0.000015)
ewo   0.000000   0.000000   0.000000 (  0.000017)
sel   0.000000   0.000000   0.000000 (  0.000016)
map   0.000000   0.000000   0.000000 (  0.000012)

And

$ jruby -v
jruby 1.7.4 (1.9.3p392) 2013-05-16 2390d3b on Java HotSpot(TM) 64-Bit Server VM 1.7.0_40-b43 +indy [darwin-x86_64]
oscar:~ ajconsta$ jruby deadhorse.rb 
Rehearsal ---------------------------------------
inj   0.030000   0.000000   0.030000 (  0.017000)
tap   0.040000   0.000000   0.040000 (  0.018000)
ewo   0.030000   0.000000   0.030000 (  0.010000)
sel   0.040000   0.000000   0.040000 (  0.015000)
map   0.050000   0.000000   0.050000 (  0.017000)
------------------------------ total: 0.190000sec

          user     system      total        real
inj   0.000000   0.000000   0.000000 (  0.001000)
tap   0.010000   0.000000   0.010000 (  0.001000)
ewo   0.000000   0.000000   0.000000 (  0.001000)
sel   0.000000   0.000000   0.000000 (  0.000000)
map   0.000000   0.000000   0.000000 (  0.001000)

@adjam
Copy link

adjam commented Nov 27, 2013

Couldn't help myself; does JIT make the order matter under jruby ('sel' moved to top)?

$ jruby deadhorse.rb 
Rehearsal ---------------------------------------
sel   0.030000   0.000000   0.030000 (  0.020000)
inj   0.010000   0.000000   0.010000 (  0.012000)
tap   0.050000   0.000000   0.050000 (  0.015000)
ewo   0.030000   0.000000   0.030000 (  0.012000)
map   0.050000   0.000000   0.050000 (  0.017000)
------------------------------ total: 0.170000sec

          user     system      total        real
sel   0.000000   0.000000   0.000000 (  0.000000)
inj   0.000000   0.000000   0.000000 (  0.000000)
tap   0.010000   0.000000   0.010000 (  0.000000)
ewo   0.000000   0.000000   0.000000 (  0.000000)
map   0.000000   0.000000   0.000000 (  0.001000)

@billdueber
Copy link

The takeaway, obviously, is that you can hardly measure the time of any of them with a million iterations so it doesn't really matter. I tried using an array of 800 elements, too, and got nothing any different.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment