[英]How to profile python 3.5 code line by line in jupyter notebook 5
How to find out execution time taken by each line of python code.如何找出每行python代码所花费的执行时间。
line_profiler works with ipython but doesnt work with jupyter notebook. line_profiler 适用于 ipython,但不适用于 jupyter notebook。 I tried adding @profile to my function, it gives error saying name 'profile' is not defined.我尝试将@profile 添加到我的函数中,它给出了错误,提示未定义名称“profile”。 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.有一种方法可以通过 time.time() 做到这一点,但我想知道是否有任何内置的分析功能可以分析我函数的每一行并显示执行时间。
def prof_function():
x=10*20
y=10+x
return (y)
You can use line_profiler
in jupyter notebook.您可以在 jupyter 笔记本中使用line_profiler
。
pip install line_profiler
安装它: pip install line_profiler
%load_ext line_profiler
在您的 jupyter notebook 中,调用: %load_ext line_profiler
prof_function
as in your example.像示例中一样定义函数prof_function
。%lprun -f prof_function prof_function()
最后,配置如下: %lprun -f prof_function prof_function()
Which will provide the output:这将提供输出:
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)
To get the execution time for each line and get a nice colour coded heatmap, I use this nice ipython magic.... https://github.com/csurfer/pyheatmagic为了获得每一行的执行时间并获得漂亮的彩色编码热图,我使用了这个漂亮的 ipython 魔法...... https://github.com/csurfer/pyheatmagic
Installation:安装:
pip install py-heat-magic pip 安装 py-heat-magic
To profile every single line in notebook:要分析笔记本中的每一行:
%load_ext heat
At the top of your 2nd cell enter this at the 1st line:在第二个单元格的顶部,在第一行输入:
%%heat
You may have issues if you have more than 2000 lines of code.如果您的代码超过 2000 行,您可能会遇到问题。
Just a summary of @SA's answer只是@SA 答案的摘要
!pip install line_profiler
%load_ext line_profiler
def func():
print('hi')
%lprun -f func func()
Install line profiler安装线分析器
conda install line_profiler
More info on http://mortada.net/easily-profile-python-code-in-jupyter.html有关http://mortada.net/easily-profile-python-code-in-jupyter.html 的更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.