使用 PyCharm(或任何其他 IDE)分析 Python 程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32926847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Profiling a python program with PyCharm (or any other IDE)
提问by Nickpick
I'm running a relatively complex python program and in it there is a montecarlo simulation which takes up most of the time. I would like to find out what part of it uses the most resources so I can potentially make it faster.
我正在运行一个相对复杂的 python 程序,其中有一个蒙特卡洛模拟,它占用了大部分时间。我想找出它的哪一部分使用最多的资源,以便我可以使其更快。
I'm using PyCharm Professional edition and tried to use the profiler, but the result is only a large list of irrelevant functions that I've never heard of.
我正在使用 PyCharm 专业版并尝试使用分析器,但结果只是一大堆我从未听说过的无关函数。
Questions: Is there a good profiler I can use that delivers meaningful results so I can see which function or keyword uses the most resources in my montecarlo simulation?
问题:是否有一个很好的分析器可以提供有意义的结果,以便我可以查看在我的蒙特卡洛模拟中哪个函数或关键字使用最多的资源?
采纳答案by shafeen
Depending on your needs and your python version, maybe you want to use something like hotshot. https://docs.python.org/2/library/hotshot.html
根据您的需要和您的 python 版本,也许您想使用类似 hotshot 的东西。https://docs.python.org/2/library/hotshot.html
EDIT:
编辑:
For python 3.4 cProfile is probably one the best options you have available but you will definitely have to filter the results with grep/sed/awk to be able to get the relevant results especially if you use libraries imported where there are a lot of internal calls occurring.
对于 python 3.4 cProfile 可能是您可用的最佳选项之一,但您肯定必须使用 grep/sed/awk 过滤结果才能获得相关结果,特别是如果您使用导入的库,其中有很多内部调用发生。
I like sorting by number of calls:
python -m cProfile -s 'calls' <your_program>.py
我喜欢按通话次数排序:
python -m cProfile -s 'calls' <your_program>.py
Now the issue in python3 with that method is the number of primitive calls that will show up if cProfile is called externally, so running it internally is probably a better idea:
现在,python3 中使用该方法的问题是,如果在外部调用 cProfile,将显示原始调用的数量,因此在内部运行它可能是一个更好的主意:
import cProfile
pr = cProfile.Profile()
pr.enable()
your_function_call()
pr.disable()
# after your program ends
pr.print_stats(sort="calls")
回答by Software Prophets
Note: As mentioned in the comments, the following applies to the paid version of PyCharm:
注意:如评论中所述,以下适用于 PyCharm 的付费版本:
If using 3.x (don't know about 2.x), I'll add to shafeen's answer and make it more PyCharm specific as per the original post. This also works better for web applications or larger applications versus simple command line programs where printing the output to stdout might be okay (still better to be able to sort different ways through PyCharm's viewer).
如果使用 3.x(不知道 2.x),我将添加到 shafeen 的答案中,并按照原始帖子使其更加 PyCharm 特定。与将输出打印到标准输出可能没问题的简单命令行程序相比,这对于 Web 应用程序或更大的应用程序也更有效(最好能够通过 PyCharm 的查看器对不同的方式进行排序)。
Indeed, do as suggested by instantiating Profile and enabling and disabling as needed. To make that useful though, you'll want to save this to a file.
事实上,按照建议通过实例化 Profile 并根据需要启用和禁用。不过,为了使它有用,您需要将其保存到文件中。
- In an outer section of your code, instantiate Profile.
- In the inner section of your code, do your profiling.
- Now, call pr.dump_stats('profile.pstat')
- 在代码的外部部分,实例化 Profile。
- 在代码的内部部分,进行分析。
- 现在,调用 pr.dump_stats('profile.pstat')
You now have a profile file that you would like to examine. Go to Tools|Open CProfile snapshot. Select profile.pstat and now you can view and sort by different headings as desired.
您现在有一个要检查的配置文件。转到工具|打开 CProfile 快照。选择 profile.pstat,现在您可以根据需要查看和排序不同的标题。
Summary
概括
import cProfile as profile
# In outer section of code
pr = profile.Profile()
pr.disable()
# In section you want to profile
pr.enable()
# code of interest
pr.disable()
# Back in outer section of code
pr.dump_stats('profile.pstat')
Open file in PyCharm's CProfile viewer.
在 PyCharm 的 CProfile 查看器中打开文件。