module Benchmark::Timing

Perform caclulations on Timing results.

Constants

MICROSECONDS_PER_SECOND

Microseconds per second.

Public Class Methods

add_second(t, s) click to toggle source

Add one second to the time represenetation

# File lib/benchmark/timing.rb, line 68
def self.add_second(t, s)
  return t + (s * MICROSECONDS_PER_SECOND)
end
clean_env() click to toggle source

Recycle used objects by starting Garbage Collector.

# File lib/benchmark/timing.rb, line 49
def self.clean_env
  # rbx
  if GC.respond_to? :run
    GC.run(true)
  else
    GC.start
  end
end
mean(samples) click to toggle source

Calculate (arithmetic) mean of given samples. @param [Array] samples Samples to calculate mean. @return [Float] Mean of given samples.

# File lib/benchmark/timing.rb, line 10
def self.mean(samples)
  sum = samples.inject(0) { |acc, i| acc + i }
  sum / samples.size
end
now() click to toggle source

Get an object that represents now and can be converted to microseconds

# File lib/benchmark/timing.rb, line 63
def self.now
  Process.clock_gettime Process::CLOCK_MONOTONIC, :float_microsecond
end
resample_mean(samples, resample_times=100) click to toggle source

Resample mean of given samples. @param [Integer] resample_times Resample times, defaults to 100. @return [Array] Resampled samples.

# File lib/benchmark/timing.rb, line 37
def self.resample_mean(samples, resample_times=100)
  resamples = []

  resample_times.times do
    resample = samples.map { samples[rand(samples.size)] }
    resamples << Timing.mean(resample)
  end

  resamples
end
stddev(samples, m=nil) click to toggle source

Calculate standard deviation of given samples. @param [Array] samples Samples to calculate standard deviation. @param [Float] m Optional mean (Expected value). @return [Float] standard deviation of given samples.

# File lib/benchmark/timing.rb, line 30
def self.stddev(samples, m=nil)
  Math.sqrt variance(samples, m)
end
time_us(before, after) click to toggle source

Return the number of microseconds between the 2 moments

# File lib/benchmark/timing.rb, line 73
def self.time_us(before, after)
  after - before
end
variance(samples, m=nil) click to toggle source

Calculate variance of given samples. @param [Float] m Optional mean (Expected value). @return [Float] Variance of given samples.

# File lib/benchmark/timing.rb, line 18
def self.variance(samples, m=nil)
  m ||= mean(samples)

  total = samples.inject(0) { |acc, i| acc + ((i - m) ** 2) }

  total / samples.size
end