发布于 2015-08-30 07:57:32 | 152 次阅读 | 评论: 0 | 来源: 网络整理
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
self._start = self._func()
end = self._func() self.elapsed += end - self._start self._start = None
@property def running(self):
return self._start is not None
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 that shows how it can be used:
# Use 1: Explicit start/stop t = Timer() t.start() countdown(1000000) t.stop() print(t.elapsed)
# Use 2: As a context manager with t:
countdown(1000000)
print(t.elapsed)
print(t2.elapsed)
This recipe provides a simple yet very useful class for making timing measurements and tracking 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 used to do it. As a general rule, the accuracy of timing measurements made with functions such as time.time() or time.clock() varies according to the operating system. In contrast, the time.perf_counter() function always uses the highest-resolution timer available 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 by the 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 fractional seconds. However, the actual value of the time doesn’t have any particular meaning. To make sense of the results, you have to call the functions twice and compute a time difference. More examples of timing and profiling are given in Recipe 14.13.