From 99c038278115f1af3acf4ff51ee114734398e010 Mon Sep 17 00:00:00 2001 From: calvin Date: Mon, 14 Nov 2005 22:50:44 +0000 Subject: [PATCH] add timeit decorator git-svn-id: https://linkchecker.svn.sourceforge.net/svnroot/linkchecker/trunk/linkchecker@2940 e7d03fd6-7b0d-0410-9947-9c21f3af8025 --- linkcheck/decorators.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/linkcheck/decorators.py b/linkcheck/decorators.py index dd76a403..3e6be425 100644 --- a/linkcheck/decorators.py +++ b/linkcheck/decorators.py @@ -38,6 +38,8 @@ def h (): import warnings import signal import os +import sys +import time def deprecated (func): @@ -131,3 +133,21 @@ def notimplemented (func): newfunc.__doc__ = func.__doc__ newfunc.__dict__.update(func.__dict__) return newfunc + + +def timeit (func, log=sys.stderr): + """ + Print execution time of the function. For quick'n'dirty profiling. + """ + def newfunc (*args, **kwargs): + """ + Execute function and print execution time. + """ + t = time.time() + func(*args, **kwargs) + print >>log, func.__name__, "took %0.2f seconds" % (time.time() - t) + newfunc.__name__ = func.__name__ + if func.__doc__ is not None: + newfunc.__doc__ = func.__doc__ + newfunc.__dict__.update(func.__dict__) + return newfunc