Class: Benchmark::Inputs::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/benchmark/inputs.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inputs, dup_inputs: false, sample_n: 10, sample_dt: NS_PER_MS * 200) ⇒ Job

Returns a new instance of Job.

Parameters:

  • inputs (Array)

    input values to yield to each benchmark action

  • dup_inputs (Boolean) (defaults to: false)

    whether input values will be dup-ed before they are passed to a #report block

  • sample_n (Integer) (defaults to: 10)

    number of samples to take when benchmarking

  • sample_dt (Integer) (defaults to: NS_PER_MS * 200)

    approximate duration of time (in nanoseconds) each sample should take when benchmarking

Raises:

  • (ArgumentError)

    if inputs is empty



67
68
69
70
71
72
73
74
75
76
# File 'lib/benchmark/inputs.rb', line 67

def initialize(inputs, dup_inputs: false, sample_n: 10, sample_dt: NS_PER_MS * 200)
  raise ArgumentError, "No inputs specified" if inputs.empty?

  @inputs = inputs
  @dup_inputs = dup_inputs
  @sample_n = sample_n
  @sample_dt = sample_dt
  @reports = []
  def_bench!
end

Instance Attribute Details

#dup_inputsBoolean

Indicates whether input values will be dup-ed before they are passed to a #report block. Defaults to false. This should be set to true if #report blocks destructively modify their arguments.

Returns:

  • (Boolean)


84
85
86
# File 'lib/benchmark/inputs.rb', line 84

def dup_inputs
  @dup_inputs
end

#reportsArray<Benchmark::Inputs::Report> (readonly)

Array of benchmark reports. Each call to #report adds an element to this array.

Returns:



111
112
113
# File 'lib/benchmark/inputs.rb', line 111

def reports
  @reports
end

#sample_dtInteger

The approximate duration of time (in nanoseconds) each sample should take when benchmarking. Defaults to 200,000 nanoseconds.

Returns:

  • (Integer)


105
106
107
# File 'lib/benchmark/inputs.rb', line 105

def sample_dt
  @sample_dt
end

#sample_nInteger

The number of samples to take when benchmarking. Defaults to 10.

Returns:

  • (Integer)


99
100
101
# File 'lib/benchmark/inputs.rb', line 99

def sample_n
  @sample_n
end

Instance Method Details

#compare!void

This method returns an undefined value.

Prints the relative speeds (from fastest to slowest) of all #report-ed benchmarks to $stdout.



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/benchmark/inputs.rb', line 154

def compare!
  return $stdout.puts("Nothing to compare!") if @reports.empty?

  @reports.sort_by!{|r| -r.ips }
  @reports.each{|r| r.slower_than!(@reports.first) }

  max_label_len = @reports.map{|r| r.label.length }.max
  format = "  %#{max_label_len}s:  %10.1f i/s"

  $stdout.puts("\nComparison:")
  @reports.each_with_index do |r, i|
    $stdout.printf(format, r.label, r.ips)
    if r.ratio
      $stdout.printf(" - %.2fx slower", r.ratio)
    elsif i > 0
      $stdout.printf(" - same-ish: difference falls within error")
    end
    $stdout.puts
  end
  $stdout.puts
end

#report(label) {|input| ... } ⇒ void

This method returns an undefined value.

Benchmarks the given block using the previously provided input values. If #dup_inputs is set to true, each input value is dup‘d before being passed to the block. The block’s iterations per second (i/s) is printed to $stdout, and a Report is added to #reports.

Parameters:

  • label (String)

    label for the benchmark

Yields:

  • (input)

    action to benchmark

Yield Parameters:

  • input (Object)

    one of the initially provided input values



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/benchmark/inputs.rb', line 126

def report(label)
  # estimate repititions
  reps = 1
  reps_time = 0
  while reps_time < @sample_dt
    reps_time = bench(reps){|x| yield(x) }
    reps *= 2
  end
  reps = ((reps / 2) * (reps_time.to_f / @sample_dt)).ceil

  # benchmark
  r = Report.new(label, reps * @inputs.length)
  i = @sample_n
  GC.start()
  while i > 0
    r.add_sample(bench(reps){|x| yield(x) } - bench(reps){|x| x })
    i -= 1
  end

  $stdout.puts(r.label)
  $stdout.printf("  %.1f i/s (\u00B1%.2f%%)\n", r.ips, r.stddev / r.ips * 100)
  @reports << r
end