Class: Benchmark::Inputs::Job

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dup_inputsBoolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/benchmark/inputs.rb', line 67

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:



87
88
89
# File 'lib/benchmark/inputs.rb', line 87

def reports
  @reports
end

#sample_dtInteger

Returns:

  • (Integer)


81
82
83
# File 'lib/benchmark/inputs.rb', line 81

def sample_dt
  @sample_dt
end

#sample_nInteger

Returns:

  • (Integer)


78
79
80
# File 'lib/benchmark/inputs.rb', line 78

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 #reports to $stdout.



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 128

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 each of the Job’s input values. If #dup_inputs is true, each input value is dup‘d before being yielded to the block. Prints the block’s estimated speed (in invocations per second) to $stdout, and adds a Report to #reports.

Parameters:

  • label (String)

    Label for the report

Yield Parameters:

  • input (Object)

    One of the Job’s input values



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/benchmark/inputs.rb', line 100

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