rewrite timecost

This commit is contained in:
yumoqing 2019-11-06 16:55:04 +08:00
parent 865438dc15
commit 8500b9155c

View File

@ -1,32 +1,33 @@
import time
import datetime
# from .Singleton import SingletonDecorator
from .Singleton import SingletonDecorator
BEGIN=0
END=1
def datetimeStr(t):
dt = time.localtime(t)
return time.strftime('%Y-%m-%d %H:%M:%S',dt)
timerecord = {}
class TimeCost:
timerecord = {}
def __init__(self,name):
self.name = name
def __enter__(self):
self.begin_time = time.time()
def __exit__(self):
def __exit__(self,*args):
self.end_time = time.time()
d = self.timerecord.get('name',[])
d = timerecord.get(self.name,[])
d.append(self.end_time - self.begin_time)
timerecord[self.name] = d
def clear(self):
timerecord = {}
def getTimeCost(self,name):
x = self.timerecord.get('name',[])
x = timerecord.get(name,[])
if len(x) == 0:
return 0,0,0
return len(x), sum(x), sum(x)/len(x)
def show(self):
for name in self.timerecord.keys():
print('TimeCost ....')
for name in timerecord.keys():
print(name, * self.getTimeCost(name))