joshsusser (owner)

Forks

Revisions

gist: 42475 Download_button fork
public
Public Clone URL: git://gist.github.com/42475.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
require 'benchmark'
 
class Validated
  def valid?
    true
  end
end
 
c1 = [Validated.new]
c10 = c1 * 10
c100 = c1 * 100
 
[c1, c10, c100].each do |c|
  Benchmark.bm(12) do |bm|
    bm.report("collect %4d" % [c.size]) { 100_000.times { c.collect { |r| r.nil? || r.valid? }.all? } }
    bm.report("inject %4d" % [c.size]) { 100_000.times { c.inject(true) { |v, r| (r.nil? || r.valid?) && v } } }
  end
end
 
# user system total real
# collect 1 0.140000 0.000000 0.140000 ( 0.150392)
# inject 1 0.200000 0.000000 0.200000 ( 0.203541)
# user system total real
# collect 10 0.700000 0.010000 0.710000 ( 0.719507)
# inject 10 1.070000 0.000000 1.070000 ( 1.087400)
# user system total real
# collect 100 6.090000 0.030000 6.120000 ( 6.216200)
# inject 100 9.800000 0.040000 9.840000 ( 10.333687)