Created
August 19, 2010 16:10
-
-
Save austinthecoder/538249 to your computer and use it in GitHub Desktop.
This file contains 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
module MethodBenchmarker | |
def benchmarks(*methods) | |
methods.flatten.each do |method| | |
define_method("#{method}_with_benchmark") do |*args| | |
r = nil | |
secs = Benchmark.realtime { r = send("#{method}_without_benchmark", *args) } | |
puts *[ | |
"\n##################################################", | |
"Method '#{method}' took #{sprintf("%.4f", secs)} seconds", | |
"##################################################" | |
] | |
r | |
end | |
alias_method_chain method, :benchmark | |
end | |
end | |
end | |
# Example: | |
# | |
# class X | |
# extend MethodBenchmarker | |
# | |
# def foo | |
# end | |
# | |
# def bar | |
# end | |
# | |
# benchmarks :foo, :bar | |
# end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment