我一直在使用cProfile来分析我的代码,它一直很好用.我还使用gprof2dot.py来显示结果(使其更清晰).
但是,cProfile(以及到目前为止我见过的大多数其他Python分析器)似乎只在函数调用级别进行分析.当从不同的地方调用某些函数时,这会引起混淆 - 我不知道呼叫#1或呼叫#2是否占用了大部分时间.当所讨论的函数深度为六级时,这会变得更糟,从其他七个地方调用.
如何进行逐行分析?
而不是这个:
function #12, total time: 2.0s
Run Code Online (Sandbox Code Playgroud)
我想看到这样的事情:
function #12 (called from somefile.py:102) 0.5s
function #12 (called from main.py:12) 1.5s
Run Code Online (Sandbox Code Playgroud)
cProfile确实显示了总共有多少时间"转移"到父级,但是当你有一堆层和互连的调用时,这种连接又会丢失.
理想情况下,我希望有一个GUI来解析数据,然后向我显示我的源文件,每个行的总时间.像这样的东西:
main.py:
a = 1 # 0.0s
result = func(a) # 0.4s
c = 1000 # 0.0s
result = func(c) # 5.0s
Run Code Online (Sandbox Code Playgroud)
然后我就可以点击第二个"func(c)"调用来查看该调用中占用的时间,与"func(a)"调用分开.
那有意义吗?是否有任何分析库收集此类信息?我错过了一些很棒的工具吗?
Question:
I've profiled my Python program to death, and there is one function that is slowing everything down. It uses Python dictionaries heavily, so I may not have used them in the best way. If I can't get it running faster, I will have to re-write it in C++, so is there anyone who can help me optimise it in Python?
I hope I've given the right sort of explanation, and that you can make some sense of my code! …
我有一个大文件,我正在读取,并将每几行转换为一个对象的实例.
由于我循环遍历文件,因此我使用list.append(instance)将实例存储到列表中,然后继续循环.
这是一个约100MB左右的文件,因此它不会太大,但随着列表变大,循环逐渐减慢.(我打印循环中每圈的时间).
这不是循环所固有的〜当我循环浏览文件时打印每个新实例时,程序以恒定速度进行〜只有当我将它们附加到列表时才会变慢.
我的朋友建议在while循环之前禁用垃圾收集,然后启用它并进行垃圾收集调用.
有没有其他人观察到list.append变慢的类似问题?有没有其他方法来规避这个?
我将尝试以下两个建议.
(1)"预先分配"记忆〜这样做的最佳方法是什么?(2)尝试使用deque
多个帖子(请参阅Alex Martelli的评论)建议内存碎片化(他有像我这样的大量可用内存)〜但没有明显的性能修复.
要复制这种现象,请运行下面答案中提供的测试代码,并假设这些列表包含有用的数据.
gc.disable()和gc.enable()有助于计时.我还会仔细分析所有时间花在哪里.
我想使用cProfile在Python中分析函数的方法.我尝试了以下方法:
import cProfile as profile
# Inside the class method...
profile.run("self.myMethod()", "output_file")
Run Code Online (Sandbox Code Playgroud)
但它不起作用.如何用"run"调用self.method?
我曾经使用过系统监视器应用程序中内置的漂亮的Apple探查器.只要您的C++代码是使用调试信息编译的,您就可以对正在运行的应用程序进行采样,并打印出一个缩进的树,告诉您父函数在此函数中花费的时间百分比(以及正文与其他函数调用) .
例如,如果main调用function_1and function_2,function_2调用function_3,然后是main调用function_3:
main (100%, 1% in function body):
function_1 (9%, 9% in function body):
function_2 (90%, 85% in function body):
function_3 (100%, 100% in function body)
function_3 (1%, 1% in function body)
Run Code Online (Sandbox Code Playgroud)
我会看到这一点,然后想一想,"有些东西需要花费很长时间function_2才能完成代码.如果我希望我的程序更快,那就是我应该开始的地方."
我怎样才能最轻松地获得Python程序的精确分析输出?
我见过有人说这样做:
import cProfile, pstats
prof = cProfile.Profile()
prof = prof.runctx("real_main(argv)", globals(), locals())
stats = pstats.Stats(prof)
stats.sort_stats("time") # Or cumulative
stats.print_stats(80) # 80 = how many to print
Run Code Online (Sandbox Code Playgroud)
但与优雅的呼叫树相比,它相当混乱.如果你能轻易做到这一点,请告诉我,这会有所帮助.
在我的源代码中,我尝试捕获并测量Python中段的时间释放.如何以方便的方式精确测量该段传递时间?
我使用Python(v2.4)profile模块来分析numpy脚本,以下条目似乎占了大部分执行时间:
ncalls tottime percall cumtime percall filename:lineno(function)
256/1 0.000 0.000 7.710 7.710 <string>:1(?)
Run Code Online (Sandbox Code Playgroud)
不幸的是,它的外观让谷歌很难.
我该如何弄清楚这究竟是什么?
编辑分析器从shell运行,如下所示:python -m profile -s cumulative script.py
我想创建一个装饰器,用于描述方法并记录结果.如何才能做到这一点?
我正在运行一个相对复杂的python程序,其中有一个montecarlo模拟,它占用了大部分时间.我想找出它使用最多资源的部分,以便我可以更快地完成它.
我正在使用PyCharm Professional版本并尝试使用分析器,但结果只是我从未听说过的大量无关功能.
问题:我是否可以使用可以提供有意义结果的优秀分析器,以便我可以看到哪个函数或关键字在我的montecarlo模拟中使用了最多的资源?
python ×9
profiling ×6
profiler ×4
performance ×2
append ×1
class ×1
cprofile ×1
decorator ×1
dictionary ×1
gprof ×1
line-by-line ×1
list ×1
optimization ×1
profile ×1
pycharm ×1
tree ×1