Module: Benchmark::Compare

Included in:
Benchmark
Defined in:
lib/benchmark/compare.rb

Instance Method Summary collapse

Instance Method Details

#compare(*reports) ⇒ Object



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
31
32
33
34
35
36
37
38
39
40
# File 'lib/benchmark/compare.rb', line 4

def compare(*reports)
  return if reports.size < 2

  iter = false
  sorted = reports.sort do |a,b|
    if a.respond_to? :ips
      iter = true
      b.ips <=> a.ips
    else
      a.runtime <=> b.runtime
    end
  end

  best = sorted.shift

  STDOUT.puts "\nComparison:"

  if iter
    STDOUT.printf "%20s: %10.1f i/s\n", best.label, best.ips
  else
    STDOUT.puts "#{best.rjust(20)}: #{best.runtime}s"
  end

  sorted.each do |report|
    name = report.label

    if iter
      x = (best.ips.to_f / report.ips.to_f)
      STDOUT.printf "%20s: %10.1f i/s - %.2fx slower\n", name, report.ips, x
    else
      x = "%.2f" % (report.ips.to_f / best.ips.to_f)
      STDOUT.puts "#{name.rjust(20)}: #{report.runtime}s - #{x}x slower"
    end
  end

  STDOUT.puts
end