Module: Benchmark

Defined in:
lib/avgbm.rb

Class Method Summary collapse

Class Method Details

.avgbm(iterations = 5, label_width = 0) {|job| ... } ⇒ Object

:yield: job

Yields:

  • (job)


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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/avgbm.rb', line 6

def self.avgbm(iterations = 5, label_width = 0) # :yield: job
  job = Job.new(label_width)
  yield(job)
  width = job.width + 1

  hash = Hash.new { |hash, key| hash[key] = [] }
 
  # benchmark
  job.list.each { |label,item|
    iterations.times do
      hash[label.to_s] << Benchmark.measure(label, &item)
    end
  }

  # minimum 5 iterations
  iterations = iterations < 5 ? 5 : iterations

  # discard the lowest and highest times
  lower_bound = iterations / 5
  upper_bound = iterations - lower_bound

  hash.each_pair do |label, reports|
    # discard the highest and lowest times
    reports.sort! {|x, y| y.real <=> x.real}
    hash[label] = reports[lower_bound...upper_bound]

    # average the remaining
    hash[label] = hash[label].inject{ |sum, el| sum + el } / hash[label].size
  end

  # print the results
  print ' '*width + CAPTION

  hash.each_pair do |label, reports|
    print label.ljust(width)
    print reports
  end

  hash
end