rewrite timecost
This commit is contained in:
parent
865438dc15
commit
8500b9155c
@ -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))
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user