File: compare.rb

package info (click to toggle)
ruby-benchmark-ips 1.2.0%2Bgit.20130609.e47e416-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 116 kB
  • ctags: 39
  • sloc: ruby: 308; makefile: 13
file content (41 lines) | stat: -rw-r--r-- 874 bytes parent folder | download
1
2
3
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
41
module Benchmark
  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

  module_function :compare
end