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