When profiling it can be useful to log the amount of time that is spent in a function. With Python that is super easy to do with decorators.
logtime will log the time spent in the function to syslog.
Download the source.
logtime.py
#!/usr/bin/python
import time
import syslog
def logtime(func):
def caller(*args, **kwargs):
stime = time.time()
ret = func(*args, **kwargs)
syslog.syslog(
syslog.LOG_LOCAL2 | syslog.LOG_INFO,
'%s=%s\n' % (func.__name__, time.time() - stime))
return ret
return caller
@logtime
def test_func(arg1, arg2=None):
print arg1, arg2
time.sleep(1)
if __name__ == '__main__':
test_func(1, 2)
Jul 14 15:05:01 olomai python: test_func=1.00114893913







Be the first to comment.