💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## 问题 You want to be able to record the time it takes to perform various tasks. ## 解决方案 The time module contains various functions for performing timing-related functions.However, it’s often useful to put a higher-level interface on them that mimics a stopwatch. For example: import time class Timer:def __init__(self, func=time.perf_counter):self.elapsed = 0.0self._func = funcself._start = Nonedef start(self):if self._start is not None:raise RuntimeError(‘Already started') self._start = self._func() def stop(self):if self._start is None:raise RuntimeError(‘Not started') end = self._func()self.elapsed += end - self._startself._start = None def reset(self):self.elapsed = 0.0 @propertydef running(self): > return self._start is not None def __enter__(self):self.start()return selfdef __exit__(self, [*](#)args):self.stop() This class defines a timer that can be started, stopped, and reset as needed by the user.It keeps track of the total elapsed time in the elapsed attribute. Here is an example thatshows how it can be used: def countdown(n):while n > 0:n -= 1 # Use 1: Explicit start/stopt = Timer()t.start()countdown(1000000)t.stop()print(t.elapsed) # Use 2: As a context managerwith t: > countdown(1000000) print(t.elapsed) with Timer() as t2:countdown(1000000) print(t2.elapsed) ## 讨论 This recipe provides a simple yet very useful class for making timing measurements andtracking elapsed time. It’s also a nice illustration of how to support the context-management protocol and the with statement.One issue in making timing measurements concerns the underlying time function usedto do it. As a general rule, the accuracy of timing measurements made with functionssuch as time.time() or time.clock() varies according to the operating system. Incontrast, the time.perf_counter() function always uses the highest-resolution timeravailable on the system.As shown, the time recorded by the Timer class is made according to wall-clock time,and includes all time spent sleeping. If you only want the amount of CPU time used bythe process, use time.process_time() instead. For example: t = Timer(time.process_time)with t: > countdown(1000000) print(t.elapsed) Both the time.perf_counter() and time.process_time() return a “time” in fractionalseconds. However, the actual value of the time doesn’t have any particular meaning. Tomake sense of the results, you have to call the functions twice and compute a timedifference.More examples of timing and profiling are given in Recipe 14.13.