Skip to content

Instantly share code, notes, and snippets.

@joshsusser
Created January 2, 2009 06:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshsusser/42475 to your computer and use it in GitHub Desktop.
Save joshsusser/42475 to your computer and use it in GitHub Desktop.
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment