我如何使用 line_profiler(来自 Robert Kern)? [英] How do I use line_profiler (from Robert Kern)?
问题描述
我曾尝试使用 line_profiler 模块通过 Python 文件获取逐行配置文件.这是我到目前为止所做的:
I have tried using the line_profiler module for getting a line-by-line profile over a Python file. This is what I've done so far:
1) 使用 .exe 文件从 pypi 安装 line_profiler(我使用的是 WinXP 和Win7).只需点击安装向导即可.
1) Installed line_profiler from pypi by using the .exe file (I am on WinXP and Win7). Just clicked through the installation wizard.
2) 编写一小段代码(类似于另一个已回答的问题 这里).
2) Written a small piece of code (similar to what has been asked in another answered question here).
from line_profiler import LineProfiler
def do_stuff(numbers):
print numbers
numbers = 2
profile = LineProfiler(do_stuff(numbers))
profile.print_stats()
3) 从 IDLE/PyScripter 运行代码.我只有时间.
3) Run the code from IDLE/PyScripter. I got only the time.
Timer unit: 4.17188e-10 s
如何在我执行的代码上获得完整的逐行配置文件?我从来没有使用过像装饰器这样的任何高级 Python 功能,所以我很难理解我应该如何使用像 这里 和 此处.
How do I get full line-by-line profile over the code I execute? I have never used any advanced Python features like decorators, so it is hard for me to understand how shall I use the guidelines provided by several posts like here and here.
推荐答案
只需从第一个 link,但使用您的代码.安装 line_profiler
模块后,您要做的就是在您希望逐行分析的每个函数之前添加一个 @profile
装饰器,并确保每个函数都被调用至少在代码中的其他地方一次——所以对于你的简单示例代码来说,应该是这样的:
Just follow Dan Riti's example from the first link, but use your code. All you have to do after installing the line_profiler
module is add a @profile
decorator right before each function you wish to profile line-by-line and make sure each one is called at least once somewhere else in the code—so for your trivial example code that would be something like this:
example.py
文件:
@profile
def do_stuff(numbers):
print numbers
numbers = 2
do_stuff(numbers)
完成后,通过安装在 C:\Python27\ 中的
目录.这是在 Windows 7 命令行会话中执行此操作的(不是很有趣的)实际输出:kernprof.py
✶ 运行您的脚本脚本
Having done that, run your script via the kernprof.py
✶ that was installed in your C:\Python27\Scripts
directory. Here's the (not very interesting) actual output from doing this in a Windows 7 command-line session:
> python "C:\Python27\Scripts\kernprof.py" -l -v example.py
2
Wrote profile results to example.py.lprof
Timer unit: 3.2079e-07 s
File: example.py
Function: do_stuff at line 2
Total time: 0.00185256 s
Line # Hits Time Per Hit % Time Line Contents
==============================================================
1 @profile
2 def do_stuff(numbers):
3 1 5775 5775.0 100.0 print numbers
您可能需要调整最后一步——使用 kernprof.py
运行测试脚本,而不是直接通过 Python 解释器运行——以便在 IDLE 或 PyScripter 中执行等效操作.
You likely need to adapt this last step—the running of your test script with kernprof.py
instead of directly by the Python interpreter—in order to do the equivalent from within IDLE or PyScripter.
✶更新
✶Update
似乎在 line_profiler
v1.0 中,kernprof
实用程序作为可执行文件分发,而不是作为 .py
脚本文件分发是我写上面的时候.这意味着现在需要使用以下命令从命令行调用它:
It appears that in line_profiler
v1.0, the kernprof
utility is distributed as an executable, not a .py
script file as it was when I wrote the above. This means the following now needs to used to invoke it from the command-line:
> "C:\Python27\Scripts\kernprof.exe" -l -v example.py
这篇关于我如何使用 line_profiler(来自 Robert Kern)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!