Module: Benchmark::Perf::Stats

Defined in:
lib/benchmark/perf/stats.rb

Class Method Summary collapse

Class Method Details

.average(measurements) ⇒ Float

Calculate arithemtic average of measurements

Parameters:

  • measurements (Array[Float])

Returns:

  • (Float)

    the average of given measurements



14
15
16
17
18
# File 'lib/benchmark/perf/stats.rb', line 14

def average(measurements)
  return 0 if measurements.empty?

  measurements.reduce(&:+).to_f / measurements.size
end

.stdev(measurements) ⇒ Object

Calculate standard deviation

Parameters:

  • measurements (Array[Float])


44
45
46
47
48
# File 'lib/benchmark/perf/stats.rb', line 44

def stdev(measurements)
  return 0 if measurements.empty?

  Math.sqrt(variance(measurements))
end

.variance(measurements) ⇒ Float

Calculate variance of measurements

Parameters:

  • measurements (Array[Float])

Returns:

  • (Float)


28
29
30
31
32
33
34
35
36
# File 'lib/benchmark/perf/stats.rb', line 28

def variance(measurements)
  return 0 if measurements.empty?

  avg = average(measurements)
  total = measurements.reduce(0) do |sum, x|
    sum + (x - avg)**2
  end
  total.to_f / measurements.size
end