Created
February 11, 2010 13:09
-
-
Save vjt/301485 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# bench() - (C) 2010 [email protected] - MIT License | |
# | |
# Useful bench() routine for your .irbrc | |
# | |
# Usage: | |
# | |
# bench { some code } | |
# 1000 iterations: 1.205242 (0.000121 per iteration) | |
# bench(4939) { some code } | |
# 4939 iterations: 1.420919 (0.000288 per iteration) | |
# | |
module Kernel | |
def bench(n = 1000, &block) | |
raise ArgumentError, 'What should I benchmark?' unless block | |
require 'benchmark' | |
print "#{n} iterations: " | |
time = Benchmark.measure { n.times(&block) }.real | |
puts "%.06f (%.06f per iteration)" % [time, time / n] | |
rescue LoadError | |
puts "Benchmark is not available in this session" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment