无法使cProfile在IPython中工作
我错过了一些非常基本的东西无法使cProfile在IPython中工作,python,profiling,profiler,ipython,Python,Profiling,Profiler,Ipython,我错过了一些非常基本的东西 class C: def __init__(self): self.N = 100 pass def f(self, param): print 'C.f -- param' for k in xrange(param): for i in xrange(self.N): for j in xrange(self.N):
class C:
def __init__(self):
self.N = 100
pass
def f(self, param):
print 'C.f -- param'
for k in xrange(param):
for i in xrange(self.N):
for j in xrange(self.N):
a = float(i)/(1+float(j)) + float(i/self.N) ** float(j/self.N)
import cProfile
c = C()
cProfile.run('c.f(3)')
当我在IPython中运行上述代码时,我得到:
NameError: name 'c' is not defined
我错过了什么
更新我的会话的确切粘贴如下:
更新我没有提到问题发生在IPython中,而IPython(事实证明)是问题的根源虽然IPython非常方便,但它破坏工作代码或掩盖错误的情况非常罕见。因此,当您遇到此类神秘错误时,在标准解释器中尝试代码是非常有用的。在IPython中,您可以使用: [9]中的
:%prun c.f(3)
C.f——参数
在0.066 CPU秒内进行3次函数调用
订购人:内部时间
ncalls tottime percall cumtime percall文件名:lineno(函数)
1 0.066 0.066 0.066 0.066:6(f)
1 0.000 0.000 0.066 0.066 :1()
1 0.000 0.000 0.000 0.000{方法'disable'的''lsprof.Profiler'对象}
这不是原始海报的问题,但如果您在_umain__;名称空间以外的其他位置(从函数或导入中)调用cProfile.run(),也会出现同样的错误。在这种情况下,您需要使用以下方法,而不是run()方法:
感谢你帮我解决了这个问题。啊!Winner,我简直不敢相信,如果不编辑我的代码,就没有办法从shell中进行分析。哇,太棒了!我不知道“%prun magic function”的%prun:)死链接,我还没有找到它应该更新到什么地方。@retracile:谢谢提醒。链接已修复。对于现在阅读本文的任何人,链接再次更改:现在链接是:
In [9]: %prun c.f(3)
C.f -- param
3 function calls in 0.066 CPU seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.066 0.066 0.066 0.066 <string>:6(f)
1 0.000 0.000 0.066 0.066 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
cProfile.runctx("your code", globals(), locals())