Print Execution Time Using Custom Decorator

 

Code Example


# Print Execution Time Using Custom Decorator 


from datetime import datetime

def print_time(fun):
    def wrapper_method(*args, **kwargs):
        start_time = datetime.now()
        res = fun(*args, **kwargs)
        end_time = datetime.now()
        print("execution time : ", end_time-start_time)
        print(res)
    return wrapper_method


@print_time
def my_function1(goto=10):    # with params
    for i in range(goto):
        i = i**1000
    return "200"


@print_time
def my_function2():           # without params
    for i in range(10):
        i = i**1000


my_function1(1000)
my_function2()



Comments

Popular posts from this blog

How To Create .ENV File in Python

Create a Large file for Test Data Processing using Python

How to solve the Liner Equation using Python