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
|
|
|
|
2019-07-16 16:33:07 +08:00
|
|
|
def getTimeCost(self,name):
|
2019-11-06 16:55:04 +08:00
|
|
|
x = timerecord.get(name,[])
|
|
|
|
if len(x) == 0:
|
|
|
|
return 0,0,0
|
2019-11-06 15:10:02 +08:00
|
|
|
return len(x), sum(x), sum(x)/len(x)
|
2019-07-16 16:33:07 +08:00
|
|
|
|
|
|
|
def show(self):
|
2019-11-06 16:55:04 +08:00
|
|
|
print('TimeCost ....')
|
|
|
|
for name in timerecord.keys():
|
2019-11-06 15:10:02 +08:00
|
|
|
print(name, * self.getTimeCost(name))
|
2019-07-16 16:33:07 +08:00
|
|
|
|