Using a Decorator to Measure Execution Time of a Function in Python

For profiling your application

Let’s assume that our function is:

def my_func(a, b):
retval = a+b
return retval

Now, we want to measure the time taken by this function. Simply, we add some lines to achieve this.

import time

def my_func(a, b):
start_time = time.time();
retval = a+b
print("the function ends in ", time.time()-start_time, "secs")
return retval

Storing the current time in start_time variable and then I let the method execute and then subtract the start_time from the current time to get the actual method execution time.

Output:

my_func(1, 2)
the function ends in 7.152557373046875e-07 secs

But we can do better using decorations to wrap any function.

from functools import wraps
import time

def timer(func):
@wraps(func)
def wrapper(a, b):
start_time = time.time();
retval = func(a, b)
print("the function ends in ", time.time()-start_time, "secs")
return retval
return wrapper

Now we can use this timer decorator to decorate the my_func function.

@timer
def my_func(a, b):
retval = a+b
return retval

When calling this function, we get the following result.

my_func(1, 2) 
the function ends in 1.1920928955078125e-06 secs

What if the function takes more than two variables since we don’t know how many arguments we are going to need?

Simply, we use *args allows us to pass a variable number of non-keyword arguments to a Python function.

def timer(func):
@wraps(func)
def wrapper(*args):
start_time = time.time();
retval = func(*args)
print("the function ends in ", time.time()-start_time, "secs")
return retval
return wrapper

We can add @timer to each of our functions for which we want to measure.

@timer
def my_func(a, b, c):
retval = a+b+c
return retval

Output:

my_func(1,2, 3)
the function ends in 1.6689300537109375e-06 secs

Easy, right?


Using a Decorator to Measure Execution Time of a Function in Python was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.

For profiling your application

Let's assume that our function is:

def my_func(a, b):
retval = a+b
return retval

Now, we want to measure the time taken by this function. Simply, we add some lines to achieve this.

import time

def my_func(a, b):
start_time = time.time();
retval = a+b
print("the function ends in ", time.time()-start_time, "secs")
return retval

Storing the current time in start_time variable and then I let the method execute and then subtract the start_time from the current time to get the actual method execution time.

Output:

my_func(1, 2)
the function ends in 7.152557373046875e-07 secs

But we can do better using decorations to wrap any function.

from functools import wraps
import time

def timer(func):
@wraps(func)
def wrapper(a, b):
start_time = time.time();
retval = func(a, b)
print("the function ends in ", time.time()-start_time, "secs")
return retval
return wrapper

Now we can use this timer decorator to decorate the my_func function.

@timer
def my_func(a, b):
retval = a+b
return retval

When calling this function, we get the following result.

my_func(1, 2) 
the function ends in 1.1920928955078125e-06 secs

What if the function takes more than two variables since we don’t know how many arguments we are going to need?

Simply, we use *args allows us to pass a variable number of non-keyword arguments to a Python function.

def timer(func):
@wraps(func)
def wrapper(*args):
start_time = time.time();
retval = func(*args)
print("the function ends in ", time.time()-start_time, "secs")
return retval
return wrapper

We can add @timer to each of our functions for which we want to measure.

@timer
def my_func(a, b, c):
retval = a+b+c
return retval

Output:

my_func(1,2, 3)
the function ends in 1.6689300537109375e-06 secs

Easy, right?


Using a Decorator to Measure Execution Time of a Function in Python was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


Print Share Comment Cite Upload Translate
APA
Anh T. Dang | Sciencx (2024-03-28T19:28:24+00:00) » Using a Decorator to Measure Execution Time of a Function in Python. Retrieved from https://www.scien.cx/2022/11/14/using-a-decorator-to-measure-execution-time-of-a-function-in-python/.
MLA
" » Using a Decorator to Measure Execution Time of a Function in Python." Anh T. Dang | Sciencx - Monday November 14, 2022, https://www.scien.cx/2022/11/14/using-a-decorator-to-measure-execution-time-of-a-function-in-python/
HARVARD
Anh T. Dang | Sciencx Monday November 14, 2022 » Using a Decorator to Measure Execution Time of a Function in Python., viewed 2024-03-28T19:28:24+00:00,<https://www.scien.cx/2022/11/14/using-a-decorator-to-measure-execution-time-of-a-function-in-python/>
VANCOUVER
Anh T. Dang | Sciencx - » Using a Decorator to Measure Execution Time of a Function in Python. [Internet]. [Accessed 2024-03-28T19:28:24+00:00]. Available from: https://www.scien.cx/2022/11/14/using-a-decorator-to-measure-execution-time-of-a-function-in-python/
CHICAGO
" » Using a Decorator to Measure Execution Time of a Function in Python." Anh T. Dang | Sciencx - Accessed 2024-03-28T19:28:24+00:00. https://www.scien.cx/2022/11/14/using-a-decorator-to-measure-execution-time-of-a-function-in-python/
IEEE
" » Using a Decorator to Measure Execution Time of a Function in Python." Anh T. Dang | Sciencx [Online]. Available: https://www.scien.cx/2022/11/14/using-a-decorator-to-measure-execution-time-of-a-function-in-python/. [Accessed: 2024-03-28T19:28:24+00:00]
rf:citation
» Using a Decorator to Measure Execution Time of a Function in Python | Anh T. Dang | Sciencx | https://www.scien.cx/2022/11/14/using-a-decorator-to-measure-execution-time-of-a-function-in-python/ | 2024-03-28T19:28:24+00:00
https://github.com/addpipe/simple-recorderjs-demo