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上下文管理器包裝想要計時的代碼塊。它將會在你的代碼塊開始執行的時候啟動計時器,在你的代碼塊結束的時候停止計時器。
這是一個使用上述代碼片段的例子: