跳转到主要内容

计时代码块

项目描述

harrison

计时代码块。

用作with语句的上下文表达式

>>> from harrison import Timer
>>> with Timer() as t:
>>>     ...
>>> print t.elapsed_time_ms
12345

当初始化时传递一个描述字符串,完成时会打印经过的时间,以该描述为键。

>>> with Timer('My expensive block of code'):
>>>     ...
My expensive block of code: 12345 ms

您还可以显式开始和停止计时器

timer = Timer()
timer.start()

some_expensive_function(...)
print timer.elapsed_time_s

another_expensive_function(...)
timer.stop()
print timer.elapsed_time_s

您还可以使用装饰器来计时函数的每次执行

from harrison import profile

@profile('Describes the function')
def some_function():
    pass

# Without args, the function name (e.g. 'some_function') will be used
# as the description.
@profile()
def another_function():
    pass

您还可以使用RegisteredTimer,它将多个命名计时器组合在一起,提供序列化它们的时间的实用程序,以及一个可选的全局计时器注册表。

以英国木匠和钟表匠约翰·哈里森的名字命名,他发明了海上计时器

类似库

这与contexttimer库类似,但该库的许可证是GPLv3,比这里使用的双条款BSD许可证更为限制性。

开发

pip install -r requirements_dev.txt
rake lint

贡献

欢迎提交拉取请求!

支持

如果您遇到问题,请告诉我们。

许可证

该项目采用双条款BSD许可证。

项目详情


下载文件

下载您平台对应的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源代码分发

metaboharrison-1.2.0.tar.gz (5.9 kB 查看哈希值)

上传时间: 源代码

由以下组织支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面