如何找出每行python代码占用的执行时间。
line_profiler可用于ipython,但不适用于jupyter笔记本。我尝试将@profile添加到我的函数中,它给出错误消息,提示未定义名称'profile'。有一种方法可以通过time.time()来执行,但是我想知道是否有任何内置的性能分析函数可以对函数的每一行进行配置并显示执行时间。
def prof_function():
x=10*20
y=10+x
return (y)
您可以line_profiler
在jupyter笔记本中使用。
pip install line_profiler
%load_ext line_profiler
prof_function
示例中的函数。%lprun -f prof_function prof_function()
它将提供输出:
Timer unit: 1e-06 s
Total time: 3e-06 s
File: <ipython-input-22-41854af628da>
Function: prof_function at line 1
Line # Hits Time Per Hit % Time Line Contents
==============================================================
1 def prof_function():
2 1 1.0 1.0 33.3 x=10*20
3 1 1.0 1.0 33.3 y=10+x
4 1 1.0 1.0 33.3 return (y)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句