如何逐行分析 Python 代码?
我一直在使用 cProfile 来分析我的代码,并且效果很好。我还使用 gprof2dot.py 来可视化结果(使其更清晰) 。
然而,cProfile(以及我迄今为止见过的大多数其他 Python 分析器)似乎只在函数调用级别进行分析。当从不同的地方调用某些函数时,这会导致混乱 - 我不知道调用 #1 或调用 #2 是否占用了大部分时间。当相关函数深度为六层并从其他七个地方调用时,情况会变得更糟。
如何获得逐行分析?
而不是这样:
function #12, total time: 2.0s
我希望看到这样的内容:
function #12 (called from somefile.py:102) 0.5s
function #12 (called from main.py:12) 1.5s
cProfile 确实显示了总时间有多少“传输”到父级,但是当您有一堆层和互连调用时,此连接会再次丢失。
理想情况下,我希望有一个 GUI 可以解析数据,然后向我显示我的源文件以及每行的总时间。像这样的事情:
main.py:
a = 1 # 0.0s
result = func(a) # 0.4s
c = 1000 # 0.0s
result = func(c) # 5.0s
然后我可以单击第二个“func(c)”调用来查看该调用中占用时间的内容,与“func(a)”调用分开。这有道理吗?
I've been using cProfile to profile my code, and it's been working great. I also use gprof2dot.py to visualize the results (makes it a little clearer).
However, cProfile (and most other Python profilers I've seen so far) seem to only profile at the function-call level. This causes confusion when certain functions are called from different places - I have no idea if call #1 or call #2 is taking up the majority of the time. This gets even worse when the function in question is six levels deep, called from seven other places.
How do I get a line-by-line profiling?
Instead of this:
function #12, total time: 2.0s
I'd like to see something like this:
function #12 (called from somefile.py:102) 0.5s
function #12 (called from main.py:12) 1.5s
cProfile does show how much of the total time "transfers" to the parent, but again this connection is lost when you have a bunch of layers and interconnected calls.
Ideally, I'd love to have a GUI that would parse through the data, then show me my source file with a total time given to each line. Something like this:
main.py:
a = 1 # 0.0s
result = func(a) # 0.4s
c = 1000 # 0.0s
result = func(c) # 5.0s
Then I'd be able to click on the second "func(c)" call to see what's taking up time in that call, separate from the "func(a)" call. Does that make sense?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我相信这就是 Robert Kern 的 line_profiler 的目的。从链接:
I believe that's what Robert Kern's line_profiler is intended for. From the link:
您还可以使用 pprofile(pypi)。
如果您想分析整个执行过程,则不需要修改源代码。
您还可以通过两种方式分析较大程序的子集:
在到达代码中的特定点时切换分析,例如:
从调用堆栈异步切换分析(需要一种在所考虑的应用程序中触发此代码的方法,例如信号处理程序或可用的工作线程)通过使用统计分析:
代码注释输出格式很像行profiler:
请注意,因为 pprofile 不依赖于代码修改,所以它可以分析顶级模块语句,从而允许分析程序启动时间(导入模块、初始化全局变量等需要多长时间)。
它可以生成cachegrind格式的输出,因此您可以使用kcachegrind轻松浏览大型结果。
披露:我是 pprofile 作者。
You could also use pprofile(pypi).
If you want to profile the entire execution, it does not require source code modification.
You can also profile a subset of a larger program in two ways:
toggle profiling when reaching a specific point in the code, such as:
toggle profiling asynchronously from call stack (requires a way to trigger this code in considered application, for example a signal handler or an available worker thread) by using statistical profiling:
Code annotation output format is much like line profiler:
Note that because pprofile does not rely on code modification it can profile top-level module statements, allowing to profile program startup time (how long it takes to import modules, initialise globals, ...).
It can generate cachegrind-formatted output, so you can use kcachegrind to browse large results easily.
Disclosure: I am pprofile author.
只是为了改进@Joe Kington的上述答案。
对于 Python 3.x,使用 line_profiler:
安装:
用法:
假设您有程序
main.py
以及其中的函数您想要根据时间进行分析的 fun_a()
和fun_b()
;您需要在函数定义之前使用装饰器@profile
。例如,可以通过执行 shell 命令来分析程序:
可以使用 $ kernprof -h 获取参数
结果将在控制台上打印为:
编辑:分析器的结果可以是使用 TAMPPA 包进行解析。使用它,我们可以得到逐行所需的图:
Just to improve @Joe Kington 's above-mentioned answer.
For Python 3.x, use line_profiler:
Installation:
Usage:
Suppose you have the program
main.py
and within it, functionsfun_a()
andfun_b()
that you want to profile with respect to time; you will need to use the decorator@profile
just before the function definitions. For e.g.,The program can be profiled by executing the shell command:
The arguments can be fetched using
$ kernprof -h
The results will be printed on the console as:
EDIT: The results from the profilers can be parsed using the TAMPPA package. Using it, we can get line-by-line desired plots as
您可以借助 line_profiler 软件包来实现此
1。第一次安装软件包:
2.使用magic命令将包加载到你的python/notebook环境
3.如果您想分析函数的代码,那么
如下操作:
如果您按照以下步骤操作,您将获得包含所有详细信息的良好格式化输出:)
You can take help of line_profiler package for this
1. 1st install the package:
2. Use magic command to load the package to your python/notebook environment
3. If you want to profile the codes for a function then
do as follows:
you will get a nice formatted output with all the details if you follow these steps :)
PyVmMonitor 有一个实时视图,可以为您提供帮助(您可以连接到正在运行的程序并从中获取统计信息)。
请参阅:http://www.pyvmonitor.com/
PyVmMonitor has a live-view which can help you there (you can connect to a running program and get statistics from it).
See: http://www.pyvmmonitor.com/