Skip to content

Instantly share code, notes, and snippets.

@vjt
Created February 11, 2010 13:09
Show Gist options
  • Save vjt/301485 to your computer and use it in GitHub Desktop.
Save vjt/301485 to your computer and use it in GitHub Desktop.
# 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