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/project/timing.rb, line 70
def self.add_second(t, s)
  t + s
end
clean_env() click to toggle source

Recycle used objects by starting Garbage Collector.

# File lib/project/timing.rb, line 35
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/project/timing.rb, line 10
def self.mean(samples)
  sum = samples.inject(:+)
  sum / samples.size
end
now() click to toggle source
# Return the number of microseconds between the 2 moments
def self.time_us(before, after)
  after - before
end

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

# File lib/project/timing.rb, line 65
def self.now
  Time.now
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/project/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/project/timing.rb, line 75
def self.time_us(before, after)
  (after.to_f - before.to_f) * MICROSECONDS_PER_SECOND
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/project/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