File: benchmark_ips_extension.rb

package info (click to toggle)
ruby-hamlit 2.15.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,996 kB
  • sloc: ruby: 10,548; ansic: 536; sh: 23; makefile: 8
file content (43 lines) | stat: -rw-r--r-- 1,152 bytes parent folder | download | duplicates (6)
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
42
43
# Monkey patch to show milliseconds
module Benchmark
  module IPS
    class Report
      module EntryExtension
        def body
          return super if Benchmark::IPS.options[:format] != :human

          left = "%s i/s (%1.3fms)" % [Helpers.scale(ips), (1000.0 / ips)]
          iters = Helpers.scale(@iterations)

          if @show_total_time
            left.ljust(20) + (" - %s in %10.6fs" % [iters, runtime])
          else
            left.ljust(20) + (" - %s" % iters)
          end
        end
      end
      Entry.prepend(EntryExtension)
    end
  end

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

      sorted = reports.sort_by(&:ips).reverse
      best = sorted.shift
      $stdout.puts "\nComparison:"
      $stdout.printf "%20s: %10.1f i/s (%1.3fms)\n", best.label, best.ips, (1000.0 / best.ips)

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

        x = (best.ips.to_f / report.ips.to_f)
        $stdout.printf "%20s: %10.1f i/s (%1.3fms) - %.2fx slower\n", name, report.ips, (1000.0 / report.ips), x
      end

      $stdout.puts
    end
  end
  extend CompareExtension
end