Created
March 9, 2011 10:11
-
-
Save vjt/861991 to your computer and use it in GitHub Desktop.
Benchmark made easy
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
# Benchmark made easy | |
# | |
# bench { stuff } # runs stuff 1000 times and returns the real time spent doing it | |
# bench(100) { stuff } # as above, but 100 times. | |
# | |
# - [email protected] - public domain | |
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