记录程序执行的时间-python cookbook(第3版)高清中文完整版

时间:2024-06-29 23:06:48
【文件属性】:

文件名称:记录程序执行的时间-python cookbook(第3版)高清中文完整版

文件大小:4.84MB

文件格式:PDF

更新时间:2024-06-29 23:06:48

python cookbook 第3版 高清 中文完整版

13.13 记录程序执行的时间 问题 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 stop watch. For example: import time class Timer: def __init__(self, func=time.perf_counter): self.elapsed = 0.0 self._func = func self._start = None def 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._start self._start = None def reset(self): self.elapsed = 0.0 @property def running(self): return self._start is not None def __enter__(self):


网友评论