module Benchmark::Perf::Iteration

Measure number of iterations a work could take in a second

@api private

Public Class Methods

call_times(times) { || ... } click to toggle source

Call work by given times

@param [Integer] times

the times to call

@return [Integer]

the number of times worke has been called

@api private

# File lib/benchmark/perf/iteration.rb, line 22
def call_times(times)
  i = 0
  while i < times
    yield
    i += 1
  end
end
cycles_per_100ms(iterations, time_s) click to toggle source

Calcualte the number of cycles needed for 100ms

@param [Integer] iterations @param [Float] time_s

the total time for all iterations in seconds

@return [Integer]

the cycles per 100ms

@api private

# File lib/benchmark/perf/iteration.rb, line 41
def cycles_per_100ms(iterations, time_s)
  cycles = iterations * Clock::MICROSECONDS_PER_100MS
  cycles /= time_s * Clock::MICROSECONDS_PER_SECOND
  cycles = cycles.to_i
  cycles <= 0 ? 1 : cycles
end
run(time: 2, warmup: 1, &work) click to toggle source

Run measurements

@param [Numeric] time

the time to run measurements in seconds

@param [Numeric] warmup

the warmup time in seconds

@api public

# File lib/benchmark/perf/iteration.rb, line 80
def run(time: 2, warmup: 1, &work)
  cycles_in_100ms = run_warmup(warmup: warmup, &work)

  GC.start

  result = IPSResult.new

  target = (before = Clock.now) + time

  while Clock.now < target
    time_s = Clock.measure { call_times(cycles_in_100ms, &work) }

    next if time_s <= 0.0 # Iteration took no time

    result.add(time_s, cycles_in_100ms)
  end

  result
end
run_warmup(warmup: 1, &work) click to toggle source

Warmup run

@param [Numeric] warmup

the number of seconds for warmup

@api private

# File lib/benchmark/perf/iteration.rb, line 55
def run_warmup(warmup: 1, &work)
  GC.start

  target = Clock.now + warmup
  iter = 0

  time_s = Clock.measure do
    while Clock.now < target
      call_times(1, &work)
      iter += 1
    end
  end

  cycles_per_100ms(iter, time_s)
end

Private Instance Methods

call_times(times) { || ... } click to toggle source

Call work by given times

@param [Integer] times

the times to call

@return [Integer]

the number of times worke has been called

@api private

# File lib/benchmark/perf/iteration.rb, line 22
def call_times(times)
  i = 0
  while i < times
    yield
    i += 1
  end
end
cycles_per_100ms(iterations, time_s) click to toggle source

Calcualte the number of cycles needed for 100ms

@param [Integer] iterations @param [Float] time_s

the total time for all iterations in seconds

@return [Integer]

the cycles per 100ms

@api private

# File lib/benchmark/perf/iteration.rb, line 41
def cycles_per_100ms(iterations, time_s)
  cycles = iterations * Clock::MICROSECONDS_PER_100MS
  cycles /= time_s * Clock::MICROSECONDS_PER_SECOND
  cycles = cycles.to_i
  cycles <= 0 ? 1 : cycles
end
run(time: 2, warmup: 1, &work) click to toggle source

Run measurements

@param [Numeric] time

the time to run measurements in seconds

@param [Numeric] warmup

the warmup time in seconds

@api public

# File lib/benchmark/perf/iteration.rb, line 80
def run(time: 2, warmup: 1, &work)
  cycles_in_100ms = run_warmup(warmup: warmup, &work)

  GC.start

  result = IPSResult.new

  target = (before = Clock.now) + time

  while Clock.now < target
    time_s = Clock.measure { call_times(cycles_in_100ms, &work) }

    next if time_s <= 0.0 # Iteration took no time

    result.add(time_s, cycles_in_100ms)
  end

  result
end
run_warmup(warmup: 1, &work) click to toggle source

Warmup run

@param [Numeric] warmup

the number of seconds for warmup

@api private

# File lib/benchmark/perf/iteration.rb, line 55
def run_warmup(warmup: 1, &work)
  GC.start

  target = Clock.now + warmup
  iter = 0

  time_s = Clock.measure do
    while Clock.now < target
      call_times(1, &work)
      iter += 1
    end
  end

  cycles_per_100ms(iter, time_s)
end