All pastes #804229 Raw Edit

Mine

public text v1 · immutable
·published 2007-12-03 07:06 UTC
rendered paste body
class Array
  def sum
    if block_given?
      inject(0) {|sum, x| sum + yield(x) }
    else
      inject(0) {|sum, x| sum + x }
    end
  end

  def mean(&block)
    sum(&block).to_f / size
  end

  def variance
    # E(x^2) - E(x)^2
    sum{|x| x ** 2 }.to_f / (size - 1) - mean ** 2
    # E( (x - e(x))^2 )
    # sum(x^2)/(size-1) - mean^2
  end

  def deviation
    Math.sqrt(variance)
  end
end

class Compare
  def initialize(command, runs)
    @runs = runs
    @name = command.match(/ (.+)/)[1]
    @array = (1..@runs).map { `#{command}`.scan(/\d+:/).last.chop.to_i }
  end

  def output
    puts "script: #{@name} runs: #{@runs}  iterations -> average: #{@array.mean} standard deviation: #{@array.deviation}"
  end
end

if ARGV.empty? then
  puts "Usage: ruby #{$0} <runs>"
  exit
else
  runs = ARGV[0].to_i
end

time = Time.now

Compare.new("ruby original.rb", runs).output
#Compare.new("runhaskell original.hs").output

puts "Total time: #{Time.now - time} seconds"