modify timecost

This commit is contained in:
yumoqing 2019-11-06 15:10:02 +08:00
parent 55f53e34ae
commit 865438dc15

View File

@ -1,5 +1,6 @@
import time import time
import datetime import datetime
# from .Singleton import SingletonDecorator
BEGIN=0 BEGIN=0
END=1 END=1
@ -9,30 +10,23 @@ def datetimeStr(t):
return time.strftime('%Y-%m-%d %H:%M:%S',dt) return time.strftime('%Y-%m-%d %H:%M:%S',dt)
class TimeCost: class TimeCost:
def __init__(self): timerecord = {}
self.timerecord = {} def __init__(self,name):
self.name = name
def begin(self,name): def __enter__(self):
self.timerecord[name] = {'begin':time.time()} self.begin_time = time.time()
def end(self,name): def __exit__(self):
if name not in self.timerecord.keys(): self.end_time = time.time()
self.timerecord[name] = {'begin':time.time()} d = self.timerecord.get('name',[])
d = self.timerecord[name] d.append(self.end_time - self.begin_time)
d['end'] = time.time()
def getTimeCost(self,name): def getTimeCost(self,name):
return self.timerecord[name]['end'] - self.timerecord[name]['begin'] x = self.timerecord.get('name',[])
return len(x), sum(x), sum(x)/len(x)
def getTimeBegin(self,name):
return self.timerecord[name]['begin']
def getTimeEnd(self,name):
return self.timerecord[name]['end']
def show(self): def show(self):
for name in self.timerecord.keys(): for name in self.timerecord.keys():
d = self.timerecord[name] print(name, * self.getTimeCost(name))
cost = d['end'] - d['begin']
print(name,'begin:',datetimeStr(d['begin']),'end:',datetimeStr(d['end']),'cost:%f seconds' % cost)