如何从cProfile获取真实耗时?
我在用cProfile,想要找出在某个函数以及整个程序中花费的实际时间(也就是墙钟时间)。我在 使用自定义计时器 的文档中看到它写道
...强制使用墙钟时间或经过的进程时间...
这让我觉得它不一定提供墙钟时间。怎么获取墙钟时间?像 tottime 这样的值是不是墙钟时间?
如果这有关系的话,我使用 python3 -m cProfile,因为我的代码是自动生成的。
解决方案
默认的 tottime 和 cumtime 确实是墙钟时间。
如果你需要CPU时钟,请使用 cProfile.Profile(timer=time.process_time)
代码可能是这样的:
import cProfile
import time
profiler = cProfile.Profile(timer=time.process_time) # CPU time only
profiler.run('your_code()')
profiler.print_stats()
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。