Python decorator to measure the execution time of methods

Fahim Sakri
Apr 21, 2017 · 2 min read

Being a python developer you will always in need for profiling the application. In my current project my goal was figure out the bottleneck in the code. For that i had track the execution time for each methods.

So in the beginning, i started by storing the current time in start_time variable and then i let the method execute and then subtract the start_time with current time to get the actual method execution time as mentioned below:

start_time = int(round(time.time() * 1000))
employees = Employee.get_all_employee_details()
time_diff = current_milli_time() — start_time debug_log_time_diff.update({'FETCH_TIME': time_diff})

There is no issue with the above line of code but you know, this will significantly keep on increasing when you have many method/function call. At one point, it will be difficult to figure out the actual code and what you will see is the repetition of this code.

To overcome this, created the @timeit decorator which allows you to measure the execution time of the method/function by just adding the @timeit decorator on the method.

@timeit decorator:

def timeit(method):
def timed(*args, **kw):
ts = time.time()
result = method(*args, **kw)
te = time.time()
if 'log_time' in kw:
name = kw.get('log_name', method.__name__.upper())
kw['log_time'][name] = int((te - ts) * 1000)
else:
print '%r %2.2f ms' % \
(method.__name__, (te - ts) * 1000)
return result
return timed

Adding decorator to the method

@timeit
def get_all_employee_details(**kwargs):
print 'employee details'

The code will look like this after removing the redundant code.

logtime_data ={}
employees = Employee.get_all_employee_details(log_time=logtime_data)

Hurray!! All that messy code is gone and now it looks simple and clear.

log_time and log_name are optional. Make use of them accordingly when needed.

Other articles

Reference

Python Decorator for execution time

P.S. Thanks for reading this far! If you found value in this, I’d really appreciate it if you recommend this post (by clicking the ❤ button) so other people can see it!.

PythonHive

PythonHive on Medium should be some kind of living room for…

PythonHive

PythonHive on Medium should be some kind of living room for Python Developers and hard core users, who want to share there experiences and knowledge with each other.

Fahim Sakri

Written by

Works at @Fynd

PythonHive

PythonHive on Medium should be some kind of living room for Python Developers and hard core users, who want to share there experiences and knowledge with each other.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store