Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Ruby script to calculate statistics from CSV data file
#!/usr/bin/env ruby -n
# usage: ruby timestats.rb < data.csv
BEGIN {
$data = { real: [], user: [], sys: [] }
class Array
def mean
sum = inject(0) { |s,n| s += n }
sum.to_f / size
end
def median
if size % 2 == 0
sort[(size/2)-1, 2].mean
else
sort[(size/2)]
end
end
def stddev
m = mean
variance = inject(0) { |s,n| s+= (n-m) ** 2 } / (size-1)
Math.sqrt(variance)
end
end
}
END {
puts " real user sys"
puts "min %6.2f %6.2f %6.2f" % $data.values.map(&:min)
puts "max %6.2f %6.2f %6.2f" % $data.values.map(&:max)
puts "mean %6.2f %6.2f %6.2f" % $data.values.map(&:mean)
puts "stddev %6.2f %6.2f %6.2f" % $data.values.map(&:stddev)
puts "median %6.2f %6.2f %6.2f" % $data.values.map(&:median)
}
next unless $_ =~ /^([\d\.]+),([\d\.]+),([\d\.]+)$/
real, user, sys = $1, $2, $3
$data[:real] << real.to_f
$data[:user] << user.to_f
$data[:sys] << sys.to_f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment