From 523fb62d4b05e5fb25feac4dab7a2d3124439856 Mon Sep 17 00:00:00 2001 From: han0 Date: Tue, 1 Jun 2021 15:49:16 +0800 Subject: [PATCH] =?UTF-8?q?fix(runtimer):=20=E4=BF=AE=E5=A4=8D=E5=BC=82?= =?UTF-8?q?=E6=AD=A5=E6=89=A7=E8=A1=8C=E6=97=B6=E8=AE=A1=E6=97=B6=E5=99=A8?= =?UTF-8?q?=E8=AE=A1=E6=97=B6=E5=BC=82=E5=B8=B8=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nc_http/tools/runtimer.py | 42 ++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/nc_http/tools/runtimer.py b/nc_http/tools/runtimer.py index 194a230..4343ea8 100644 --- a/nc_http/tools/runtimer.py +++ b/nc_http/tools/runtimer.py @@ -2,27 +2,37 @@ class RunTimer: """ 运行计时器 :example: - >>> RunTimer.set_point() - >>> for i in range(1000): - >>> print(i) - >>> RunTimer.set_point() - >>> RunTimer.check() + >>> timer = RunTimer() + >>> timer.set_point() + >>> time.sleep(1) + >>> time_cost = timer.check_one() + >>> 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 profile = [] - @classmethod - def set_point(cls): + def set_point(self): import time time = time.time() - if cls.time: - cls.profile.append(time - cls.time) - cls.time = time + if self.time: + self.profile.append(time - self.time) + self.time = time - @classmethod - def check(cls): - print(cls.profile) - cls.time = None - profile = cls.profile.copy() - cls.profile = [] + def check(self): + self.set_point() + self.time = None + profile = self.profile.copy() + self.profile = [] return profile + + def check_one(self): + profile = self.check() + return profile[0]