Class: Benchmark::Inputs::Job
- Inherits:
-
Object
- Object
- Benchmark::Inputs::Job
- Defined in:
- lib/benchmark/inputs.rb
Instance Attribute Summary collapse
-
#dup_inputs ⇒ Boolean
Indicates whether input values will be
dup-ed before they are passed to a #report block. -
#reports ⇒ Array<Benchmark::Inputs::Report>
readonly
Array of benchmark reports.
-
#sample_dt ⇒ Integer
The approximate duration of time (in nanoseconds) each sample should take when benchmarking.
-
#sample_n ⇒ Integer
The number of samples to take when benchmarking.
Instance Method Summary collapse
-
#compare! ⇒ void
Prints the relative speeds (from fastest to slowest) of all #report-ed benchmarks to $stdout.
-
#initialize(inputs, dup_inputs: false, sample_n: 10, sample_dt: NS_PER_MS * 200) ⇒ Job
constructor
A new instance of Job.
-
#report(label) {|input| ... } ⇒ void
Benchmarks the given block using the previously provided input values.
Constructor Details
#initialize(inputs, dup_inputs: false, sample_n: 10, sample_dt: NS_PER_MS * 200) ⇒ Job
Returns a new instance of Job.
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_inputs ⇒ Boolean
84 85 86 |
# File 'lib/benchmark/inputs.rb', line 84 def dup_inputs @dup_inputs end |
#reports ⇒ Array<Benchmark::Inputs::Report> (readonly)
Array of benchmark reports. Each call to #report adds an element to this array.
111 112 113 |
# File 'lib/benchmark/inputs.rb', line 111 def reports @reports end |
#sample_dt ⇒ Integer
The approximate duration of time (in nanoseconds) each sample should take when benchmarking. Defaults to 200,000 nanoseconds.
105 106 107 |
# File 'lib/benchmark/inputs.rb', line 105 def sample_dt @sample_dt end |
#sample_n ⇒ Integer
The number of samples to take when benchmarking. Defaults to 10.
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.
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 |