Module: Benchmark::Timing

Defined in:
lib/benchmark/timing.rb

Overview

Perform caclulations on Timing results.

Class Method Summary collapse

Class Method Details

.clean_envObject

Recycle used objects by starting Garbage Collector.



47
48
49
50
51
52
53
54
# File 'lib/benchmark/timing.rb', line 47

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.



8
9
10
11
# File 'lib/benchmark/timing.rb', line 8

def self.mean(samples)
  sum = samples.inject(0) { |acc, i| acc + i }
  sum / samples.size
end

.resample_mean(samples, resample_times = 100) ⇒ Array

Resample mean of given samples.

Parameters:

  • resample_times (Integer) (defaults to: 100)

    Resample times, defaults to 100.

Returns:

  • (Array)

    Resampled samples.



35
36
37
38
39
40
41
42
43
44
# File 'lib/benchmark/timing.rb', line 35

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) ⇒ 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.



28
29
30
# File 'lib/benchmark/timing.rb', line 28

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.



16
17
18
19
20
21
22
# File 'lib/benchmark/timing.rb', line 16

def self.variance(samples, m=nil)
  m ||= mean(samples)

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

  total / samples.size
end