From 8500b9155c79283a9d78bb4a11f591755df680b5 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Wed, 6 Nov 2019 16:55:04 +0800 Subject: [PATCH] rewrite timecost --- appPublic/timecost.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/appPublic/timecost.py b/appPublic/timecost.py index 5b611fd..5cb9615 100755 --- a/appPublic/timecost.py +++ b/appPublic/timecost.py @@ -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))