测量代码块的执行时间

block-timer的Python项目详细描述


PyPiMITTravisCICoverageUpdatesSupported Python implementationsSupported Python versions

安装

  • 支持的python版本有:3.43.53.63.7
pip install --upgrade block-timer

用法

您可以使用Timer类作为上下文管理器或具有块计时器的方法/函数装饰器来轻松度量代码块:

将使用标准print函数打印经过的时间:

fromblock_timer.timerimportTimerwithTimer():pass# Some operation# Total time ... seconds will be printed

如果有多个代码块,可以设置title属性:

fromblock_timer.timerimportTimerwithTimer(title="Block A"):pass# Some operation# [Block A] Total time ... seconds will be printedwithTimer(title="Block B"):pass# Some operation# [Block B] Total time ... seconds will be printed

通过elapsed属性可以访问已用时间(以小数秒为单位)。您也可以通过print_title=False

禁用打印
fromblock_timer.timerimportTimerwithTimer(print_title=False)ast:pass# Some operationprint("Elapsed time: {:f} seconds".format(t.elapsed))

您可以重定向打印功能的输出:

importsysfromblock_timer.timerimportTimerwithTimer(print_file=sys.stdout):pass# Some operation

Timer类可以用作方法/函数装饰符:

@Timer(title="Foo")defsome_func():time.sleep(1)some_func()# [Foo] Total time ... seconds will be printed

许可证

麻省理工学院许可证(MIT)

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
AmazonS3查找从S3forJava下载的压缩文件的MIME类型   java如何使用Selenium在<span>中查找具有特定文本的元素   python如何使用OpenIEDemo生成自定义三元组。由stanfordnlp提供的java   java遇到“方法不适用”编译错误   java如何使用Mockito在Looper中运行的验证代码。getMainLooper?   类Java目录错误   java在已知其他泛型信息时使用原始类型   java connect()和disconnect()在哪里实现?   java使用PDF Box库拆分PDF,生成的PDF几乎与源PDF文件大小相同   java PowerMockito返回错误的对象   java如何找到TIBCO集合消息的字节编码?   java Basic音乐播放器下一步和上一步按钮   添加模块描述符时,java没有名为“entityManagerFactory”的bean可用   java为什么我的代码不是线程安全的?   eclipse java。引用项目中的类的lang.NoClassDefFoundError