Execution-Time-Calculator

Sun 29 June 2025
import random
import time
# PythonDecorators/entry_exit_class.py
class execution_time_calculator(object):

    def __init__(self, f):
        self.f = f

    def __call__(self):
        start = int(round(time.time() * 1000))
        print("Entering", self.f.__name__)
        self.f()
        end = int(round(time.time() * 1000))
        #print("Exited", self.f.__name__)
        print("Time taken : "+str((end-start)) + ' milli seconds')
@execution_time_calculator
def func1():
    seconds = random.randint(5, 15)    
    print("inside func2() sleeping for "+str(seconds) + " seconds")
    time.sleep(seconds)
func1()
Entering func1
inside func2() sleeping for 15 seconds
Time taken : 15004 milli seconds


Score: 5

Category: basics