import time
[docs]class XTimer:
"""This class implements a simple timer that can be used to quickly profile
the speed of operations within library components. The timer simply uses the
system time for timing, and thus does not incur significant overhead on
runtime.
"""
DAY_MILLIS = 86400000
HOUR_MILLIS = 3600000
MINUTE_MILLIS = 60000
SECOND_MILLIS = 1000
__start = time.time() * 1000
def __init__(self):
self.__stop = self.__start
[docs] def start(self):
"""Starts the timer.
"""
self.__start = time.time() * 1000
self.__stop = self.__start
[docs] def stop(self):
"""Stops the timer (takes time).
"""
self.__stop = time.time()
[docs] def get_duration(self):
"""Retrieve the runtime of the timer.
:return: Runtime between start (or creation of timer) and stop, in milliseconds.
:rtype: int
"""
if self.__start == self.__stop:
return time.time() * 1000 - self.__stop
return self.__stop - self.__start
[docs] def get_duration_string(self):
"""Retrieve the runtime of the timer as a pretty-print string.
:return: Runtime between start (or creation of timer) and stop, as a
pretty-print string.
:rtype: str
"""
return self.format_duration(self.get_duration())