import timeit
import duo.core.duo_exception as de
#------------------------------------------------------------
#------------------------------------------------------------
[docs]class Timer:
#------------------------------------------------------------
#------------------------------------------------------------
def __init__(self):
self.start = 0.0
self.stop = 0.0
self.timeElapsed = 0.0
#------------------------------------------------------------
#------------------------------------------------------------
[docs] def StartOrResume(self):
self.start = timeit.default_timer()
#------------------------------------------------------------
#------------------------------------------------------------
[docs] def Stop(self):
self.stop = timeit.default_timer()
self.timeElapsed += self.stop - self.start
#------------------------------------------------------------
#------------------------------------------------------------
[docs] def GetElapsedTimeInSecond(self):
return self.timeElapsed
#------------------------------------------------------------
#------------------------------------------------------------
[docs]class TimerManager:
#------------------------------------------------------------
#------------------------------------------------------------
def __init__(self):
self.timerList = {}
#------------------------------------------------------------
#------------------------------------------------------------
[docs] def StartOrResume(self, name):
if name not in self.timerList.keys():
self.timerList[name] = Timer()
self.timerList[name].StartOrResume()
#------------------------------------------------------------
#------------------------------------------------------------
[docs] def Stop(self, name):
if name not in self.timerList.keys():
raise de.DuoException("--> Specified timer not found.")
self.timerList[name].Stop()
#------------------------------------------------------------
#------------------------------------------------------------
[docs] def GetElapsedTimeInSecond(self, name):
if name not in self.timerList.keys():
raise de.DuoException("--> Specified timer not found.")
return self.timerList[name].GetElapsedTimeInSecond()
#------------------------------------------------------------
#------------------------------------------------------------
[docs] def Show(self):
print("--> Timing result")
for key, value in self.timerList.items():
message = " {0:s} : {1:f} [sec]".format(key, value.GetElapsedTimeInSecond())
print(message)