如何在Jupyter Notebook 5中逐行描述python 3.5代码 [英] How to profile python 3.5 code line by line in jupyter notebook 5
问题描述
如何找出每行python代码花费的执行时间.
How to find out execution time taken by each line of python code.
line_profiler适用于ipython,但不适用于jupyter笔记本.我尝试将@profile添加到我的函数中,它给出错误消息,提示未定义名称'profile'. 有一种方法可以通过time.time()来完成,但是我想知道是否有任何内置的性能分析函数可以对函数的每一行进行分析,并向我显示执行时间.
line_profiler works with ipython but doesnt work with jupyter notebook. I tried adding @profile to my function, it gives error saying name 'profile' is not defined. There is one way to do it by time.time() , but i was wondering if there is any inbuilt profiling function which can profile each line of my function and show me the execution time.
def prof_function():
x=10*20
y=10+x
return (y)
推荐答案
您可以在jupyter笔记本中使用line_profiler
.
You can use line_profiler
in jupyter notebook.
- 安装:
pip install line_profiler
- 在您的Jupyter笔记本中,致电:
%load_ext line_profiler
- 如示例中那样定义函数
prof_function
. - 最后,配置文件如下:
%lprun -f prof_function prof_function()
- Install it:
pip install line_profiler
- Within your jupyter notebook, call:
%load_ext line_profiler
- Define your function
prof_function
as in your example. - Finally, profile as follows:
%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)
这篇关于如何在Jupyter Notebook 5中逐行描述python 3.5代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!