跳转到主要内容

计时代码块

项目描述

harrison

计时代码块。

version python version license build code style

用作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许可证更严格。

开发

./dev.py init
./dev.py test

贡献

欢迎pull请求!

支持

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

致谢

此项目由Paul Melnikow打包,当时在Body Labs工作。感谢Body Labs提供仓库转让。

许可证

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

项目详情


下载文件

下载适合您平台的应用程序。如果您不确定选择哪个,请了解有关安装包的更多信息。

源代码分发

harrison-2.0.0.tar.gz (6.2 kB 查看哈希值)

上传日期: 源代码

构建分发

harrison-2.0.0-py3-none-any.whl (8.7 kB 查看哈希值)

上传日期: Python 3

由以下机构支持