I am trying to access the time to execute a function that is inside a decorator function.
I followed the direction in this post because I was unable to get the function to take the arguments I passed to it. Now I am unsure of how to get the data from calling the function.
Here is my code:
import time
from functools import wraps
def sort_timer(func):
def outer(func):
@wraps(func)
def inner(*args, **kwargs):
start = time.perf_counter()
func(*args, **kwargs)
finish = time.perf_counter()
return start - finish
return inner
return outer
@sort_timer
def bubble_sort(a_list):
"""Sorts a_list in ascending order"""
for pass_num in range(len(a_list) - 1):
for index in range(len(a_list) - 1 - pass_num):
if a_list[index] > a_list[index + 1]:
temp = a_list[index]
a_list[index] = a_list[index + 1]
a_list[index + 1] = temp
list1 = [60, 19, 22, 14, 43, 27, 3, 77]
x = bubble_sort(list1)
print(x)
It would appear that what is being returned is the inner function. Here is what is logged to the console:
<function sort_timer.<locals>.outer.<locals>.inner at 0x0000027167770310>
Any insight would be appreciated. Thank you.