import time
from types import FunctionType
class CalcTime:
def __init__(self,name='') -> None:
self.t=0
if(isinstance(name,FunctionType)):
self.name=name
else:
self.name=str(name)
def __enter__(self):
print('in')
self.t=time.time()
def __exit__(self, type, value, trace):
v=round((time.time()-self.t)*1000)
print('out',self.name,v)
def __call__(self, *args, **kwds) :
t=time.time()
try:
self.name(*args,**kwds)
except Exception as e:
v=round((time.time()-t)*1000)
print('out',self.name.__name__,v)
raise e
v=round((time.time()-t)*1000)
print('out',self.name.__name__,v)
with CalcTime('test') :
time.sleep(0.1)
pass
@CalcTime
def ttt(a):
time.sleep(1)
print(a)