return self
10 |
|
11 |
def __exit__(self, *args): |
12 |
self.end = time.time() |
13 |
self.secs = self.end - self.start |
14 |
self.msecs = self.secs * 1000 # millisecs |
15 |
if self.verbose: |
16 |
print 'elapsed time: %f ms' % self.msecs |
为了使用它,你需要用Python的with关键字和Timer上下文管理器包装想要计时的代码块。它将会在你的代码块开始执行的时候启动计时器,在你的代码块结束的时候停止计时器。
这是一个使用上述代码片段的例子: