Skip to content

Instantly share code, notes, and snippets.

@josephruscio
Forked from mojombo/stats.rb
Created September 15, 2009 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save josephruscio/187669 to your computer and use it in GitHub Desktop.
Save josephruscio/187669 to your computer and use it in GitHub Desktop.
Refactor with aggregate gem
# 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