Module: Benchmark

Defined in:
lib/benchmark/timed_each.rb

Class Method Summary (collapse)

Class Method Details

+ (Object) timed_each(collection, options = {}, &block)

Iterate over the passed in collection, outputing benchmark info at each step

Options Hash (options):

  • :start_caption (String) — default: "Started processing collection\n"

    Output before starting

  • :finish_caption (String) — default: "Finished processing collection"

    Output when finished, on same line as total time

  • :format (String) — default: "- %r\n"

    Format String for benchmark (fmtstr)

  • :item_to_s (Symbol, String) — default: :to_s

    Output before starting



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/benchmark/timed_each.rb', line 13

def self.timed_each(collection, options={}, &block)
  caption = options[:start_caption] || "Started processing collection\n"
  finish_caption = options[:finish_caption] || "Finished processing collection"
  label_width = nil
  fmtstr = options[:format] || "- %r\n"
  item_to_s = options[:item_to_s] || :to_s
  
  benchmark(caption, label_width, fmtstr, finish_caption) do |x|
    [collection.map do |item|
      x.report("  processing #{item.send(item_to_s)} -") do
        block.call(*item)
      end
    end.inject(&:+)]
  end
end