Sha256: 22e1f996c43dd8bd33c21adb7465345994dea99400a9756c3fbaaba878707de4

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

module Benchmark
  module Perf
    module Stats
      # Calculate arithemtic average of measurements
      #
      # @param [Array[Float]] measurements
      #
      # @return [Float]
      #   the average of given measurements
      #
      # @api public
      def average(measurements)
        return 0 if measurements.empty?

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

      # Calculate variance of measurements
      #
      # @param [Array[Float]] measurements
      #
      # @return [Float]
      #
      # @api public
      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
      module_function :variance

      # Calculate standard deviation
      #
      # @param [Array[Float]] measurements
      #
      # @api public
      def stdev(measurements)
        return 0 if measurements.empty?

        Math.sqrt(variance(measurements))
      end
      module_function :stdev
    end # Stats
  end # Perf
end # Benchmark

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
benchmark-perf-0.6.0 lib/benchmark/perf/stats.rb