Module: Benchmark::Perf::Stats
- Defined in:
- lib/benchmark/perf/stats.rb
Class Method Summary collapse
-
.average(measurements) ⇒ Float
Calculate arithemtic average of measurements.
-
.stdev(measurements) ⇒ Object
Calculate standard deviation.
-
.variance(measurements) ⇒ Float
Calculate variance of measurements.
Class Method Details
.average(measurements) ⇒ Float
Calculate arithemtic average of 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
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
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 |