class Benchmark::IPS::Job

Benchmark jobs.

def compare!

Set @compare to true.
def compare!
  @compare = true
end

def compare?

Returns:
  • (Boolean) - Need to compare?
def compare?
  @compare
end

def config opts

Options Hash: (**opts)
  • :time (Integer) -- Calculation time.
  • :warmup (Integer) -- Warmup time.
def config opts
  @warmup = opts[:warmup] if opts[:warmup]
  @time = opts[:time] if opts[:time]
end

def create_report(item, measured_us, iter, avg_ips, sd_ips, cycles)

Parameters:
  • cycles (Integer) -- Number of Cycles.
  • sd_ips (Float) -- Standard deviation iterations per second.
  • avg_ips (Float) -- Average iterations per second.
  • iter (Integer) -- Iterations.
  • measured_us (Integer) -- Measured time in microsecond.
  • item (Benchmark::IPS::Job::Entry) -- Report item.
def create_report(item, measured_us, iter, avg_ips, sd_ips, cycles)
  @full_report.add_entry item.label, measured_us, iter, avg_ips, sd_ips, cycles
end

def cycles_per_100ms time_msec, iters

Returns:
  • (Integer) - Cycles per 100ms.

Parameters:
  • iters (Integer) -- Iterations.
  • time_msec (Float) -- Each iteration's time in ms.
def cycles_per_100ms time_msec, iters
  cycles = ((MICROSECONDS_PER_100MS / time_msec) * iters).to_i
  cycles = 1 if cycles <= 0
  cycles
end

def initialize opts={}

Options Hash: (**opts)
  • (false) (Boolean) -- :quiet Suppress the printing of information.
  • (nil) (Benchmark::Suite) -- :suite Specify Benchmark::Suite.
def initialize opts={}
  @suite = opts[:suite] || nil
  @quiet = opts[:quiet] || false
  @list = []
  @compare = false
  @timing = {}
  @full_report = Report.new
  # Default warmup and calculation time in seconds.
  @warmup = 2
  @time = 5
end

def item(label="", str=nil, &blk) # :yield:

Raises:
  • (ArgumentError) - Raises if str and blk are both absent.
  • (ArgumentError) - Raises if str and blk are both present.

Parameters:
  • blk (Proc) -- Code to be benchamrked.
  • str (String) -- Code to be benchamrked.
  • label (String) -- Label of benchmarked code.
def item(label="", str=nil, &blk) # :yield:
  if blk and str
    raise ArgumentError, "specify a block and a str, but not both"
  end
  action = str || blk
  raise ArgumentError, "no block or string" unless action
  @list.push Entry.new(label, action)
  self
end

def iterations_per_sec cycles, time_us

Returns:
  • (Float) - Iteration per second.

Parameters:
  • time_us (Integer) -- Time in microsecond.
  • cycles (Integer) -- Cycles.
def iterations_per_sec cycles, time_us
  MICROSECONDS_PER_SECOND * (cycles.to_f / time_us.to_f)
end

def run

Run calculation.
def run
  @list.each do |item|
    @suite.running item.label, @time if @suite
    unless @quiet
      $stdout.print item.label_rjust
    end
    Timing.clean_env
    iter = 0
    target = Time.now + @time
    measurements_us = []
    # Running this number of cycles should take around 100ms.
    cycles = @timing[item]
    while Time.now < target
      before = Time.now
      item.call_times cycles
      after = Time.now
      # If for some reason the timing said this took no time (O_o)
      # then ignore the iteration entirely and start another.
      iter_us = time_us before, after
      next if iter_us <= 0.0
      iter += cycles
      measurements_us << iter_us
    end
    final_time = Time.now
    measured_us = measurements_us.inject(0) { |a,i| a + i }
    all_ips = measurements_us.map { |time_us|
      iterations_per_sec cycles, time_us
    }
    avg_ips = Timing.mean(all_ips)
    sd_ips =  Timing.stddev(all_ips).round
    rep = create_report(item, measured_us, iter, avg_ips, sd_ips, cycles)
    if (final_time - target).abs >= (@time.to_f * MAX_TIME_SKEW)
      rep.show_total_time!
    end
    $stdout.puts " #{rep.body}" unless @quiet
    @suite.add_report rep, caller(1).first if @suite
  end
end

def run_comparison

Run comparison of entries in +@full_report+.
def run_comparison
  @full_report.run_comparison
end

def run_warmup

Run warmup.
def run_warmup
  @list.each do |item|
    @suite.warming item.label, @warmup if @suite
    unless @quiet
      $stdout.printf item.label_rjust
    end
    Timing.clean_env
    before = Time.now
    target = Time.now + @warmup
    warmup_iter = 0
    while Time.now < target
      item.call_times(1)
      warmup_iter += 1
    end
    after = Time.now
    warmup_time_us = time_us before, after
    @timing[item] = cycles_per_100ms warmup_time_us, warmup_iter
    case Benchmark::IPS.options[:format]
    when :human
      $stdout.printf "%s i/100ms\n", Helpers.scale(@timing[item]) unless @quiet
    else
      $stdout.printf "%10d i/100ms\n", @timing[item] unless @quiet
    end
    @suite.warmup_stats warmup_time_us, @timing[item] if @suite
  end
end

def time_us before, after

Returns:
  • (Float) - Time difference of before and after.

Parameters:
  • after (Time) -- time.
  • before (Time) -- time.
def time_us before, after
  (after.to_f - before.to_f) * MICROSECONDS_PER_SECOND
end