-
-
Save josephruscio/187669 to your computer and use it in GitHub Desktop.
Refactor with aggregate gem
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
# Using the aggregate gem (http://www.gemcutter.org/gems/aggregate), | |
# run the given block +num+ times and then print out the mean, min, | |
# max, std_dev, and histogram of the run duration. For example: | |
# | |
# irb> stats(100, 0, 10, 1) { sleep(rand / 100) } | |
# mean: 5.99ms | |
# min: 1.49ms | |
# max: 9.28ms | |
# stddev: 2.54ms | |
# value |------------------------------------------------------------------| count | |
# 0 |@@ | 2 | |
# 1 |@@@@@@@@@@@@@ | 13 | |
# 2 |@@@@@@@@@@ | 10 | |
# 3 |@@@@@@@@@@ | 10 | |
# 4 |@@@@@@@@@@@@ | 12 | |
# 5 |@@@@@@@@@@@ | 11 | |
# 6 |@@@@@@@@@@@@@ | 13 | |
# 7 |@@@@@ | 5 | |
# 8 |@@@@@@@@@@@@ | 12 | |
# 9 |@@@@@@@@ | 8 | |
# Total |------------------------------------------------------------------| 96 | |
require 'rubygems' | |
require 'aggregate' | |
def stats(num, low=nil, high=nil, width=nil) | |
records = Aggregate.new(low, high, width) | |
num.times do | |
t0 = Time.now | |
yield | |
records << (Time.now - t0) * 1000 | |
end | |
puts "mean: %1.2fms" % records.mean | |
puts "min: %1.2fms" % records.min | |
puts "max: %1.2fms" % records.max | |
puts "stddev: %1.2fms" % records.std_dev | |
puts records.to_s | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment