使用 PyCharm(或任何其他 IDE)分析 Python 程序 [英] Profiling a python program with PyCharm (or any other IDE)

查看:49
本文介绍了使用 PyCharm(或任何其他 IDE)分析 Python 程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个相对复杂的 python 程序,其中有一个蒙特卡洛模拟,它占用了大部分时间.我想知道它的哪一部分使用的资源最多,以便我可以让它更快.

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.

我使用的是 PyCharm 专业版并尝试使用分析器,但结果只是一大堆我从未听说过的无关函数.

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.

问题:是否有一个很好的分析器可以提供有意义的结果,以便我可以查看在我的蒙特卡洛模拟中哪个函数或关键字使用的资源最多?

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?

推荐答案

根据您的需求和您的 Python 版本,也许您想使用类似 hotshot 之类的东西.https://docs.python.org/2/library/hotshot.html

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 3.4 cProfile 可能是您可用的最佳选择之一,但您肯定必须使用 grep/sed/awk 过滤结果才能获得相关结果,特别是如果您使用导入的库,其中有很多发生的内部呼叫.

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 -m cProfile -s 'calls' <your_program>.py

I like sorting by number of calls: python -m cProfile -s 'calls' <your_program>.py

现在使用该方法在 python3 中的问题是如果在外部调用 cProfile 将显示原始调用的数量,因此在内部运行它可能是一个更好的主意:

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:

import cProfile

pr = cProfile.Profile()
pr.enable()
your_function_call()
pr.disable()
# after your program ends
pr.print_stats(sort="calls")

这篇关于使用 PyCharm(或任何其他 IDE)分析 Python 程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆