# module Benchmark::Timing

Perform calculations 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 54
    def self.add_second(t, s)
      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 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 numbers. @param [Array] samples Samples to calculate mean. @return [Float] Mean of given numbers.

    # File lib/benchmark/timing.rb, line 10
    def self.mean(samples)
      sum = samples.inject(:+)
      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 49
    def self.now
      Process.clock_gettime Process::CLOCK_MONOTONIC, :float_microsecond
    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 59
    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