apppublic/appPublic/timecost.py

43 lines
788 B
Python
Raw Normal View History

2019-07-16 16:33:07 +08:00
import time
import datetime
2019-11-06 16:55:04 +08:00
from .Singleton import SingletonDecorator
2019-07-16 16:33:07 +08:00
2019-11-06 16:55:04 +08:00
timerecord = {}
2019-11-06 15:10:02 +08:00
2019-07-16 16:33:07 +08:00
class TimeCost:
2019-11-06 15:10:02 +08:00
def __init__(self,name):
self.name = name
2019-07-16 16:33:07 +08:00
2019-11-06 15:10:02 +08:00
def __enter__(self):
self.begin_time = time.time()
2019-07-16 16:33:07 +08:00
2019-11-06 16:55:04 +08:00
def __exit__(self,*args):
2019-11-06 15:10:02 +08:00
self.end_time = time.time()
2019-11-06 16:55:04 +08:00
d = timerecord.get(self.name,[])
2019-11-06 15:10:02 +08:00
d.append(self.end_time - self.begin_time)
2019-11-06 16:55:04 +08:00
timerecord[self.name] = d
def clear(self):
timerecord = {}
2019-11-06 15:10:02 +08:00
2022-10-08 12:30:41 +08:00
@classmethod
def clear_all(self):
timerecord = {}
@classmethod
def clear(self, name):
timerecord[name] = []
2022-10-08 12:24:32 +08:00
@classmethod
2019-07-16 16:33:07 +08:00
def show(self):
2022-10-08 12:50:04 +08:00
def getTimeCost(name):
x = timerecord.get(name,[])
if len(x) == 0:
return 0,0,0
return len(x), sum(x), sum(x)/len(x)
2019-11-06 16:55:04 +08:00
print('TimeCost ....')
for name in timerecord.keys():
2022-10-08 12:50:04 +08:00
print(name, * getTimeCost(name))
2019-07-16 16:33:07 +08:00