Module: Benchmark::Compare

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

Overview

Functionality of performaing comparison between reports.

Usage:

Add x.compare! to perform comparison between reports.

Example:

> Benchmark.ips do |x|
  x.report('Reduce using tag')     { [*1..10].reduce(:+) }
  x.report('Reduce using to_proc') { [*1..10].reduce(&:+) }
  x.compare!
end

Calculating -------------------------------------
    Reduce using tag     19216 i/100ms
Reduce using to_proc     17437 i/100ms
-------------------------------------------------
    Reduce using tag   278950.0 (±8.5%) i/s -    1402768 in   5.065112s
Reduce using to_proc   247295.4 (±8.0%) i/s -    1238027 in   5.037299s

Comparison:
    Reduce using tag:   278950.0 i/s
Reduce using to_proc:   247295.4 i/s - 1.13x slower

Besides regular Calculating report, this will also indicates which one is slower.

Instance Method Summary collapse

Instance Method Details

#compare(*entries) ⇒ Object

Compare between reports, prints out facts of each report: runtime, comparative speed difference.

Parameters:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/benchmark/compare.rb', line 34

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

  sorted = entries.sort_by{ |e| e.stats.central_tendency }.reverse

  best = sorted.shift

  $stdout.puts "\nComparison:"

  $stdout.printf "%20s: %10.1f i/s\n", best.label, best.stats.central_tendency

  sorted.each do |report|
    name = report.label.to_s
    
    $stdout.printf "%20s: %10.1f i/s - ", name, report.stats.central_tendency
    
    best_low = best.stats.central_tendency - best.stats.error
    report_high = report.stats.central_tendency + report.stats.error
    overlaps = report_high > best_low 
    
    if overlaps
      $stdout.print "same-ish: difference falls within error"
    else
      slowdown, error = report.stats.slowdown(best.stats)
      $stdout.printf "%.2fx ", slowdown
      if error
        $stdout.printf " (± %.2f)", error
      end
      $stdout.print " slower"
    end
    
    $stdout.puts
  end

  footer = best.stats.footer
  $stdout.puts footer.rjust(40) if footer

  $stdout.puts
end