Module: Benchmark

Defined in:
lib/benchmark/inputs.rb,
lib/benchmark/inputs/version.rb

Defined Under Namespace

Modules: Inputs

Class Method Summary collapse

Class Method Details

.inputs(values, **options) {|job| ... } ⇒ Benchmark::Inputs::Job

Initializes a benchmark Job, and yields the Job to the given block.

Examples:

Benchmarking non-destructive operations

Benchmark.inputs(["abc", "aaa", "xyz", ""]) do |job|
  job.report("String#tr"){|string| string.tr("a", "A") }
  job.report("String#gsub"){|string| string.gsub(/a/, "A") }
  job.compare!
end

Benchmarking destructive operations

Benchmark.inputs(["abc", "aaa", "xyz", ""], dup_inputs: true) do |job|
  job.report("String#tr!"){|string| string.tr!("a", "A") }
  job.report("String#gsub!"){|string| string.gsub!(/a/, "A") }
  job.compare!
end

Parameters:

  • values (Array)

    Input values to be individually yielded to all report blocks

  • options (Hash)

Options Hash (**options):

  • :dup_inputs (Boolean) — default: false

    Whether each of values should be dup‘d before being yielded to a report block. This should be set to true if any report block destructively modifies its input.

  • :sample_n (Integer) — default: 10

    Number of samples to take when benchmarking

  • :sample_dt (Integer) — default: 200, 000 ns

    Approximate duration of time each sample should take when benchmarking, in nanoseconds

Yield Parameters:

Returns:

Raises:

  • (ArgumentError)

    if values is empty



38
39
40
41
42
# File 'lib/benchmark/inputs.rb', line 38

def self.inputs(values, **options)
  job = Inputs::Job.new(values, **options)
  yield job
  job
end