Python中如何计算函数的执行时间?

WBOY
发布: 2023-04-22 09:43:07
转载
2732人浏览过

python开发,有时需要做性能分析及性能优化,这时就需要记录一些耗时函数执行时间问题,然后针对函数逻辑进行优化。

在python3中一般都有哪些方法呢。

1、使用time.time()

这种方法较简单,但如果想更精确的计算函数的执行时间,会产生精度缺失,没办法统计时间极短的函数耗时。

<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">import time def func(): time.sleep(1) t = time.time() func() print(f'耗时:{time.time() - t:.4f}s') 耗时:1.0050s</pre>

登录后复制
</div>

2、使用time.perf_counter()

perf_counter是在python3.3新添加的,返回性能计数器的值,返回值是浮点型,统计结果包括睡眠的时间,单个函数的返回值无意义,只有多次运行取差值的结果才是有效的函数执行时间。

立即学习Python免费学习笔记(深入)”;

美间AI
美间AI

美间AI:让设计更简单

美间AI 45
查看详情 美间AI

<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">import time def func(): print('hello world') t = time.perf_counter() func() print(f'耗时:{time.perf_counter() - t:.8f}s') hello world 耗时:0.00051790s</pre>

登录后复制
</div>

3、使用timeit.timeit ()

<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">timeit()函数有5个参数: stmt 参数是需要执行的语句,默认为 pass setup 参数是用来执行初始化代码或构建环境的语句,默认为 pass timer 是计时器,默认是 perf_counter() number 是执行次数,默认为一百万 globals 用来指定要运行代码的命名空间,默认为 None import timeit def func(): print('hello world') print(f'耗时: {timeit.timeit(stmt=func, number=1)}') hello world 耗时: 0.0007705999999999824</pre>

登录后复制
</div>

4、使用装饰器统计

在实际项目代码中,可以通过装饰器方便的统计函数运行耗时。使用装饰器来统计函数执行耗时的好处是对函数的入侵性小,易于编写和修改。

装饰器装饰函数的方案只适用于统计函数的运行耗时,如果有代码块耗时统计的需求就不能用了,这种情况下可以使用 with 语句自动管理上下文。

(1)同步函数的统计

<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">import time def coast_time(func): def fun(*args, **kwargs): t = time.perf_counter() result = func(*args, **kwargs) print(f'函数:{func.__name__} 耗时:{time.perf_counter() - t:.8f} s') return result return fun @coast_time def test(): print('hello world') if __name__ == '__main__': test()</pre>

登录后复制
</div>

(2)异步函数的统计

<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">import asyncio import time from asyncio.coroutines import iscoroutinefunction def coast_time(func): def fun(*args, **kwargs): t = time.perf_counter() result = func(*args, **kwargs) print(f'函数:{func.__name__} 耗时:{time.perf_counter() - t:.8f} s') return result async def func_async(*args, **kwargs): t = time.perf_counter() result = await func(*args, **kwargs) print(f'函数:{func.__name__} 耗时:{time.perf_counter() - t:.8f} s') return result if iscoroutinefunction(func): return func_async else: return fun @coast_time def test(): print('hello test') time.sleep(1) @coast_time async def test_async(): print('hello test_async') await asyncio.sleep(1) if __name__ == '__main__': test() asyncio.get_event_loop().run_until_complete(test_async()) hello test 函数:test 耗时:1.00230700 s hello test_async 函数:test_async 耗时:1.00572550 s</pre>

登录后复制
</div>

5、with语句统计

通过实现 enter 和 exit 函数可以在进入和退出上下文时进行一些自定义动作,例如连接或断开数据库、打开或 关闭文件、记录开始或结束时间等,例如:我们用来统计函数块的执行时间。

with语句不仅可以统计代码块的执行时间,也可以统计函数的执行时间,还可以统计多个函数的执行时间之和,相比装饰器来说对代码的入侵性比较大,不易于修改,好处是使用起来比较灵活,不用写过多的重复代码。

<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">import asyncio import time class CoastTime(object): def __init__(self): self.t = 0 def __enter__(self): self.t = time.perf_counter() return self def __exit__(self, exc_type, exc_val, exc_tb): print(f'耗时:{time.perf_counter() - self.t:.8f} s') def test(): print('hello test') with CoastTime(): time.sleep(1) async def test_async(): print('hello test_async') with CoastTime(): await asyncio.sleep(1) if __name__ == '__main__': test() asyncio.get_event_loop().run_until_complete(test_async()) hello test 耗时:1.00723310 s hello test_async 耗时:1.00366820 s</pre>

登录后复制
</div>

以上就是Python中如何计算函数的执行时间?的详细内容,更多请关注php中文网其它相关文章!

python速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:亿速云网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号