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} " 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"