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 with the given inputs and yields that job to the given block.

Examples:

Benchmarking non-destructive operations

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

Benchmarking destructive operations

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

Parameters:

  • values (Array)

    input values to yield to each benchmark action

  • options (Hash)

Options Hash (**options):

  • :dup_inputs (Boolean)

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

  • :sample_n (Integer)

    number of samples to take when benchmarking

  • :sample_dt (Integer)

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

Yields:

  • (job)

    configures job and runs benchmarks

Yield Parameters:

Returns:

Raises:

  • (ArgumentError)

    if values is empty



41
42
43
44
45
# File 'lib/benchmark/inputs.rb', line 41

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