fix(runtimer): 修复异步执行时计时器计时异常的问题

This commit is contained in:
han0
2021-06-01 15:49:16 +08:00
parent c9ff5bc237
commit 523fb62d4b

View File

@@ -2,27 +2,37 @@ class RunTimer:
""" """
运行计时器 运行计时器
:example: :example:
>>> RunTimer.set_point() >>> timer = RunTimer()
>>> for i in range(1000): >>> timer.set_point()
>>> print(i) >>> time.sleep(1)
>>> RunTimer.set_point() >>> time_cost = timer.check_one()
>>> RunTimer.check() >>> print(time_cost)
or
>>> timer = RunTimer()
>>> timer.set_point()
>>> time.sleep(1)
>>> timer.set_point()
>>> time.sleep(2)
>>> time_cost = timer.check()
>>> print(time_cost)
""" """
time = None time = None
profile = [] profile = []
@classmethod def set_point(self):
def set_point(cls):
import time import time
time = time.time() time = time.time()
if cls.time: if self.time:
cls.profile.append(time - cls.time) self.profile.append(time - self.time)
cls.time = time self.time = time
@classmethod def check(self):
def check(cls): self.set_point()
print(cls.profile) self.time = None
cls.time = None profile = self.profile.copy()
profile = cls.profile.copy() self.profile = []
cls.profile = []
return profile return profile
def check_one(self):
profile = self.check()
return profile[0]