mirror of
https://github.com/LmeSzinc/StarRailCopilot.git
synced 2024-11-23 17:11:42 +00:00
50 lines
1018 B
Python
50 lines
1018 B
Python
|
import time
|
||
|
from functools import wraps
|
||
|
|
||
|
from module.logger import logger
|
||
|
|
||
|
|
||
|
def timer(function):
|
||
|
@wraps(function)
|
||
|
def function_timer(*args, **kwargs):
|
||
|
t0 = time.time()
|
||
|
|
||
|
result = function(*args, **kwargs)
|
||
|
t1 = time.time()
|
||
|
print('%s: %s s' % (function.__name__, str(round(t1 - t0, 10))))
|
||
|
return result
|
||
|
return function_timer
|
||
|
|
||
|
|
||
|
class Timer:
|
||
|
def __init__(self, limit):
|
||
|
self.limit = limit
|
||
|
self._current = 0
|
||
|
|
||
|
def start(self):
|
||
|
if not self.started():
|
||
|
self._current = time.time()
|
||
|
|
||
|
def started(self):
|
||
|
return bool(self._current)
|
||
|
|
||
|
def current(self):
|
||
|
"""
|
||
|
Returns:
|
||
|
float
|
||
|
"""
|
||
|
return time.time() - self._current
|
||
|
|
||
|
def reached(self):
|
||
|
"""
|
||
|
Returns:
|
||
|
bool
|
||
|
"""
|
||
|
return time.time() - self._current > self.limit
|
||
|
|
||
|
def reset(self):
|
||
|
self._current = time.time()
|
||
|
|
||
|
def show(self):
|
||
|
logger.info('%s s' % str(self.current()))
|