cProfile 没有方法 runcall

PPr*_*eus 3 python cprofile python-2.7

我正在尝试使用 cProfile 来分析一些 python 代码。我相信我需要使用cProfile.runcall(),而不是cProfile.run(),因为我想要运行的方法是形式self.funct()而不是简单的funct()

当我尝试使用此处cProfile.runcall详细说明时,出现以下错误:

AttributeError: 'module' object has no attribute 'runcall'

是否从 cProfile 中删除了 runcall 方法?如果是这样,是否有使用表单的替代方法cProfile.runcall(self.funct,*args)

最小(非)工作示例:

import cProfile

def funct(a):
    print a

cProfile.runcall(funct,"Hello")
Run Code Online (Sandbox Code Playgroud)

mar*_*eau 5

在这种情况下,问题是因为runcall()Profile 实例的方法,而不是模块级函数(这是您的代码尝试使用它的方式)。您需要先构造 one 的实例,如文档中的代码片段所示。

这似乎有效(在 Python 2.7.14 中):

import cProfile

def funct(a):
    print a

pr = cProfile.Profile()
pr.enable()
pr.runcall(funct, "Hello")
Run Code Online (Sandbox Code Playgroud)