Python Timer Class - Context Manager for Timing Code Blocks
CODE:
from timeit import default_timerclass Timer(object):
def __init__(self, verbose=False):
self.verbose = verbose
self.timer = default_timer
def __enter__(self):
self.start = self.timer()
return self
def __exit__(self, *args):
end = self.timer()
self.elapsed_secs = end - self.start
self.elapsed = self.elapsed_secs * 1000 # millisecs
if self.verbose:
print 'elapsed time: %f ms' % self.elapsedTo use the Timer (context manager object), invoke it using Python's `with` statement. The duration of the context (code inside your `with` block) will be timed. It uses the appropriate timer for your platform, via the `timeit` module.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-733605/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python - Context Manager 上下文管理器PythonContext
- pythonic context manager知多少PythonContext
- Code::Blocks Debug配置BloC
- [linux]安裝code::blocksLinuxBloC
- Centos 7—安裝Code::Blocks—Codeblocks—codeblocksCentOSBloC
- hibernate.current_session_context_classSessionContext
- Code::Blocks專案配置基礎BloC
- 【暴力】codeforces 838A Binary BlocksBloC
- 用code::blocks寫C/C++程式BloCC++
- code::blocks不能除錯的可能原因BloC除錯
- 在Code::Blocks中使用C++11標準BloCC++
- with open() as 的用法 和 with上下文管理器(Context manager)Context
- sending context initialized event to listener instance of class org.springframework.web.context.ContContextZedSpringFrameworkWeb
- 關於code::blocks的幾點注意 part1BloC
- Code::Blocks 編譯引數的相關設定BloC編譯
- Blocks Programming Helper ↗ Blocks in System APIsBloCAPI
- JNI開發:Class android.content.Context could not be foundAndroidContext
- SAP WebClient UI component context node class單元測試方法WebclientUIContext
- FPGA Timing筆記FPGA筆記
- python的__class__()Python
- Python中的ClassPython
- Python list of class attributes - PythonPython
- JavaScript需要BlocksJavaScriptBloC
- CSS transition-timing-functionCSSFunction
- Python class中的otherPython
- 18、 Caused by: java.lang.NoSuchMethodException: [class android.content.Context..AttributeSetJavaExceptionAndroidContext
- 關於 Code::Blocks nightly build svn8982 的新功能介紹BloCUI
- Blocks的實現BloC
- Overview of Data Blocks(二)ViewBloC
- Dumping Oracle BlocksOracleBloC
- dba_tables.blocks、dba_tables.empty_blocks和dba_segments.blocks之間是什麼關係BloC
- python3.5如何用classPython
- python3 class的使用Python
- Cannot decode object of class Employee for key (NS.object.0); the class may be defined in source cod...Object
- (譯)窺探Blocks(2)BloC
- iOS-Blocks學習iOSBloC
- (譯)窺探Blocks (1)BloC
- CSS3 animation-timing-functionCSSS3Function