Skip to content

Instantly share code, notes, and snippets.

@vjt
Created March 9, 2011 10:11
Show Gist options
  • Save vjt/861991 to your computer and use it in GitHub Desktop.
Save vjt/861991 to your computer and use it in GitHub Desktop.
Benchmark made easy
# 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.
#
# - vjt@openssl.it - 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