Module: Benchmark::IPS

Included in:
Benchmark
Defined in:
lib/benchmark/ips.rb,
lib/benchmark/ips/job.rb,
lib/benchmark/ips/share.rb,
lib/benchmark/ips/report.rb,
lib/benchmark/ips/stats/sd.rb,
lib/benchmark/ips/job/entry.rb,
lib/benchmark/ips/stats/bootstrap.rb,
lib/benchmark/ips/job/multi_report.rb,
lib/benchmark/ips/job/stream_report.rb,
lib/benchmark/ips/stats/stats_metric.rb

Overview

Benchmark in iterations per second, no more guessing!

See Benchmark.ips for documentation on using this gem~

Defined Under Namespace

Modules: Helpers, Stats Classes: Job, Report, Share

Constant Summary collapse

VERSION =

Benchmark-ips Gem version.

"2.14.0"
CODENAME =

CODENAME of current version.

"Akagi"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.optionsObject

Set options for running the benchmarks. :format => [:human, :raw]

:human format narrows precision and scales results for readability
:raw format displays 6 places of precision and exact iteration counts


109
110
111
# File 'lib/benchmark/ips.rb', line 109

def self.options
  @options ||= {:format => :human}
end

Instance Method Details

#ips(*args) {|job| ... } ⇒ Report

Measure code in block, each code’s benchmarked result will display in iteration per second with standard deviation in given time.

Parameters:

  • time (Integer)

    Specify how long should benchmark your code in seconds.

  • warmup (Integer)

    Specify how long should Warmup time run in seconds.

Yields:

  • (job)

Returns:



33
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
73
74
75
76
77
# File 'lib/benchmark/ips.rb', line 33

def ips(*args)
  if args[0].is_a?(Hash)
    time, warmup, quiet = args[0].values_at(:time, :warmup, :quiet)
  else
    time, warmup, quiet = args
  end

  sync, $stdout.sync = $stdout.sync, true

  job = Job.new

  job_opts = {}
  job_opts[:time] = time unless time.nil?
  job_opts[:warmup] = warmup unless warmup.nil?
  job_opts[:quiet] = quiet unless quiet.nil?

  job.config job_opts

  yield job

  job.load_held_results

  job.run

  if job.run_single? && job.all_results_have_been_run?
    job.clear_held_results
  else
    job.save_held_results
    puts '', 'Pausing here -- run Ruby again to measure the next benchmark...' if job.run_single?
  end

  $stdout.sync = sync
  job.run_comparison
  job.generate_json

  report = job.full_report

  if ENV['SHARE'] || ENV['SHARE_URL']
    require 'benchmark/ips/share'
    share = Share.new report, job
    share.share
  end

  report
end

#ips_quick(*methods, on: Kernel, **opts) ⇒ Object

Quickly compare multiple methods on the same object.

Examples:

Compare String#upcase and String#downcase

ips_quick(:upcase, :downcase, on: "hello")

Compare two methods you just defined, with a custom warmup.

def add; 1+1; end
def sub; 2-1; end
ips_quick(:add, :sub, warmup: 10)

Parameters:

  • methods (Symbol...)

    A list of method names (as symbols) to compare.

  • receiver (Object)

    The object on which to call the methods. Defaults to Kernel.

  • opts (Hash)

    Additional options for customizing the benchmark.

Options Hash (**opts):

  • :warmup (Integer)

    The number of seconds to warm up the benchmark.

  • :time (Integer)

    The number of seconds to run the benchmark.



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/benchmark/ips.rb', line 93

def ips_quick(*methods, on: Kernel, **opts)
  ips(opts) do |x|
    x.compare!

    methods.each do |name|
      x.report(name) do |iter|
        iter.times { on.__send__ name }
      end
    end
  end
end