module Benchmark::Compare

def compare(*entries, order: :fastest)

Parameters:
  • entries (Array) -- Reports to compare.
def compare(*entries, order: :fastest)
  return if entries.size < 2
  case order
  when :baseline
    baseline = entries.shift
    sorted = entries.sort_by{ |e| e.stats.central_tendency }.reverse
  when :fastest
    sorted = entries.sort_by{ |e| e.stats.central_tendency }.reverse
    baseline = sorted.shift
  else
    raise ArgumentError, "Unknown order: #{order.inspect}"
  end
  $stdout.puts "\nComparison:"
  $stdout.printf "%20s: %10.1f i/s\n", baseline.label.to_s, baseline.stats.central_tendency
  sorted.each do |report|
    name = report.label.to_s
    $stdout.printf "%20s: %10.1f i/s - ", name, report.stats.central_tendency
    if report.stats.overlaps?(baseline.stats)
      $stdout.print "same-ish: difference falls within error"
    elsif report.stats.central_tendency > baseline.stats.central_tendency
      speedup, error = report.stats.speedup(baseline.stats)
      $stdout.printf "%.2fx ", speedup
      if error
        $stdout.printf " (± %.2f)", error
      end
      $stdout.print " faster"
    else
      slowdown, error = report.stats.slowdown(baseline.stats)
      $stdout.printf "%.2fx ", slowdown
      if error
        $stdout.printf " (± %.2f)", error
      end
      $stdout.print " slower"
    end
    $stdout.puts
  end
  footer = baseline.stats.footer
  $stdout.puts footer.rjust(40) if footer
  $stdout.puts
end