Created
September 15, 2009 20:07
-
-
Save mojombo/187595 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Run the given block +num+ times and then print out the mean, median, min, | |
# max, and stddev of the run. For example: | |
# | |
# irb> stats(10) { sleep(rand / 100) } | |
# mean: 5.99ms | |
# median: 6.76ms | |
# min: 1.49ms | |
# max: 9.28ms | |
# stddev: 2.54ms | |
def stats(num) | |
records = [] | |
num.times do | |
t0 = Time.now | |
yield | |
records << (Time.now - t0) | |
end | |
srecords = records.sort | |
mean = records.inject(0) { |acc, x| acc + x } / num.to_f | |
stddev = Math.sqrt(records.inject(0) { |sum, e| sum + (e - mean) ** 2 } / num.to_f ) | |
puts "mean: %1.2fms" % (mean * 1000) | |
puts "median: %1.2fms" % (srecords[num / 2] * 1000) | |
puts "min: %1.2fms" % (srecords.first * 1000) | |
puts "max: %1.2fms" % (srecords.last * 1000) | |
puts "stddev: %1.2fms" % (stddev * 1000) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment