Skip to content

Instantly share code, notes, and snippets.

@austinthecoder
Created August 19, 2010 16:10
Show Gist options
  • Save austinthecoder/538249 to your computer and use it in GitHub Desktop.
Save austinthecoder/538249 to your computer and use it in GitHub Desktop.
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