Execution-Time-Calculator
Sat 17 May 2025
title: "Execution Time Calculator" author: "Raja CSP Raman" date: 2019-04-20 description: "-" type: technical_note draft: false
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 13 seconds
Time taken : 13007 milli seconds
Score: 5
Category: basics