Skip to content

Instantly share code, notes, and snippets.

@mbklein
Forked from mjgiarlo/benchmark_dead_horse.rb
Last active December 29, 2015 14:09
Show Gist options
  • Save mbklein/7681938 to your computer and use it in GitHub Desktop.
Save mbklein/7681938 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'benchmark'
N = 10000000
vals = [1, 2, 'string', {}, [], false, true, nil]
enum = N.times.map { vals[rand(vals.length-1)] }
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 8.020000 0.030000 8.050000 ( 8.310335)
# tap 6.800000 0.040000 6.840000 ( 7.087833)
# ewo 8.360000 0.050000 8.410000 ( 8.763888)
# sel 8.700000 0.080000 8.780000 ( 8.858959)
# map 8.800000 0.070000 8.870000 ( 9.288306)
# ----------------------------- total: 40.950000sec
#
# user system total real
# inj 8.350000 0.050000 8.400000 ( 8.785699)
# tap 6.880000 0.040000 6.920000 ( 7.301747)
# ewo 7.900000 0.040000 7.940000 ( 8.359985)
# sel 8.940000 0.010000 8.950000 ( 9.086054)
# map 10.000000 0.050000 10.050000 ( 10.510230)
@adjam
Copy link

adjam commented Nov 27, 2013

$ ruby -v
ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-darwin12.5.0]

$ ruby mbk_deadhorse.rb 
Rehearsal ---------------------------------------
inj   3.040000   0.030000   3.070000 (  3.176647)
tap   2.660000   0.030000   2.690000 (  2.799970)
ewo   2.990000   0.030000   3.020000 (  3.130882)
sel   3.250000   0.010000   3.260000 (  3.252886)
map   3.580000   0.050000   3.630000 (  3.755020)
----------------------------- total: 15.670000sec

          user     system      total        real
inj   3.030000   0.030000   3.060000 (  3.168154)
tap   2.620000   0.030000   2.650000 (  2.770237)
ewo   2.980000   0.040000   3.020000 (  3.125989)
sel   3.230000   0.010000   3.240000 (  3.234174)
map   3.470000   0.040000   3.510000 (  3.623967)

$ 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]

$ jruby mbk_deadhorse.rb 
Rehearsal ---------------------------------------
inj   8.650000   0.060000   8.710000 (  5.963000)
tap   4.690000   0.020000   4.710000 (  2.315000)
ewo   4.460000   0.040000   4.500000 (  1.949000)
sel   4.720000   0.040000   4.760000 (  2.139000)
map   3.680000   0.000000   3.680000 (  2.268000)
----------------------------- total: 26.360000sec

          user     system      total        real
inj   6.640000   0.070000   6.710000 (  2.334000)
tap   1.750000   0.000000   1.750000 (  1.305000)
ewo   4.410000   0.010000   4.420000 (  1.955000)
sel   4.810000   0.010000   4.820000 (  2.176000)
map   3.210000   0.010000   3.220000 (  2.195000)

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