Sha256: 9b279cc7e4a5443f4d26e5d748278a8653d7e49716740332bc036657c350a092

Contents?: true

Size: 429 Bytes

Versions: 3

Compression:

Stored size: 429 Bytes

Contents

require "standard_deviation"
require "benchmark"

class Array
  def stdev_ruby
    total = inject :+
    mean  = total.to_f / size
    variance = inject(0) { |variance, value| variance + (value - mean) ** 2 } / (size - 1)

    Math.sqrt(variance)
  end
end

n = 1000
values = (1..10_000).map(&:to_f)

Benchmark.bm do |b|
  b.report do
    n.times { values.stdev }
  end

  b.report do
    n.times { values.stdev_ruby }
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
standard_deviation-1.0.3 benchmark/ruby_version.rb
standard_deviation-1.0.2 benchmark/ruby_version.rb
standard_deviation-1.0.1 benchmark/ruby_version.rb