We are currently using a custom Timer class (sample given below) to compute and log time taken for various pieces of modules in our Tornado app. I was exploring timeit module and wonder if we should make use of this instead.
I understand that timeit is used for ad-hoc testing of a piece of code. But, our scenario is we got to log execution time every time a method/ piece of code is called, in our logs.
I also find the following points on timeit limiting blending it in our current app code:
- It expects a string for the function call. That makes it weird to call my function every time as a string, passing it to timeit.
- I don't find any direct method to get return value of the method when I pass the function to timeit. There is a stack overflow discussion for the same. But, it looks more like a workaround and parsing a tuple every time sounds clumsy.
Should I stick with our custom Timer class? If not, how to include timeit into the code, without disrupting the code?
Any help is much appreciated! Thank you in advance.
Sample Timer Class
import time
class Timer(object):
def __init__(self, ...):
...
def __enter__(self):
self.start = time.time()
return self
def __exit__(self, *args):
self.end = time.time()
self.interval = self.end - self.start
print self.interval