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
mean{|x| x ** 2 } - mean ** 2
end
def deviation
Math.sqrt(variance)
end
end
class Compare
def initialize(command, range = 1..ARGV[0].to_i)
if ARGV.empty? then puts "Dude, pass how many iterations you wanna run for"; break; end
@name = command.match(/ (.+)\./)[0].chop.lstrip # need to replace with a better regexp
@array = range.collect { `#{command}`.scan(/\d+:/).last.chop.to_i }
end
def output
puts "#{@name} - average number of iterations: #{@array.mean} - standard deviation of iteration values: #{@array.deviation}"
puts @array.join(', ')
end
end
time = Time.now
Compare.new("ruby original.rb").output
#Compare.new("runhaskell original.hs").output
puts "#{Time.now - time} seconds"