Module: Benchmark::Timing

Defined in:
lib/benchmark/timing.rb

Overview

Perform calculations on Timing results.

Constant Summary collapse

MICROSECONDS_PER_SECOND =

Microseconds per second.

1_000_000

Class Method Summary collapse

Class Method Details

.clean_envObject

Recycle used objects by starting Garbage Collector.



35
36
37
38
39
40
41
42
# 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) ⇒ Float

Calculate (arithmetic) mean of given samples.

Parameters:

  • samples (Array)

    Samples to calculate mean.

Returns:

  • (Float)

    Mean of given samples.



10
11
12
13
# File 'lib/benchmark/timing.rb', line 10

def self.mean(samples)
  sum = samples.inject(:+)
  sum / samples.size
end

.stddev(samples, m = nil) ⇒ Float

Calculate standard deviation of given samples.

Parameters:

  • samples (Array)

    Samples to calculate standard deviation.

  • m (Float) (defaults to: nil)

    Optional mean (Expected value).

Returns:

  • (Float)

    standard deviation of given samples.



30
31
32
# File 'lib/benchmark/timing.rb', line 30

def self.stddev(samples, m=nil)
  Math.sqrt variance(samples, m)
end

.variance(samples, m = nil) ⇒ Float

Calculate variance of given samples.

Parameters:

  • m (Float) (defaults to: nil)

    Optional mean (Expected value).

Returns:

  • (Float)

    Variance of given samples.



18
19
20
21
22
23
24
# 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