module Benchmark::Timing

def self.add_second(t, s)

Add one second to the time represenetation
def self.add_second(t, s)
  t + (s * MICROSECONDS_PER_SECOND)
end

def self.add_second(t, s)

Add one second to the time represenetation
def self.add_second(t, s)
  t + s
end

def self.clean_env

Recycle used objects by starting Garbage Collector.
def self.clean_env
  # rbx
  if GC.respond_to? :run
    GC.run(true)
  else
    GC.start
  end
end

def self.mean(samples)

Returns:
  • (Float) - Mean of given samples.

Parameters:
  • samples (Array) -- Samples to calculate mean.
def self.mean(samples)
  sum = samples.inject(:+)
  sum / samples.size
end

def self.now

Get an object that represents now and can be converted to microseconds
def self.now
  Process.clock_gettime Process::CLOCK_MONOTONIC, :float_microsecond
end

def self.now

Get an object that represents now and can be converted to microseconds
def self.now
  Time.now
end

def self.stddev(samples, m=nil)

Returns:
  • (Float) - standard deviation of given samples.

Parameters:
  • m (Float) -- Optional mean (Expected value).
  • samples (Array) -- Samples to calculate standard deviation.
def self.stddev(samples, m=nil)
  Math.sqrt variance(samples, m)
end

def self.time_us(before, after)

Return the number of microseconds between the 2 moments
def self.time_us(before, after)
  after - before
end

def self.time_us(before, after)

Return the number of microseconds between the 2 moments
def self.time_us(before, after)
  (after.to_f - before.to_f) * MICROSECONDS_PER_SECOND
end

def self.variance(samples, m=nil)

Returns:
  • (Float) - Variance of given samples.

Parameters:
  • m (Float) -- Optional mean (Expected value).
def self.variance(samples, m=nil)
  m ||= mean(samples)
  total = samples.inject(0) { |acc, i| acc + ((i - m) ** 2) }
  total / samples.size
end