无法使 cProfile 在 IPython 中工作

发布于 2024-08-12 09:55:35 字数 715 浏览 4 评论 0原文

我缺少一些非常基本的东西。

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

我缺少什么?

更新我的会话的确切粘贴在这里:http://pastebin.com/f3e1b9946

更新 我没有提到问题发生在 IPython 中,(事实证明)这是问题的根源

I'm missing something very basic.

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)')

When I run the above code in IPython, I get:

NameError: name 'c' is not defined

What am I missing?

UPDATE the exact paste of my session is here: http://pastebin.com/f3e1b9946

UPDATE I didn't mention that the problem occurs in IPython, which (at it turns out) is the source of the problem

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风蛊 2024-08-19 09:55:35

在 IPython 中,您可以使用 %prun 魔法功能

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}

While inside IPython, you can use the %prun magic function:

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}
酒中人 2024-08-19 09:55:35

这不是原始海报的问题,但如果您在 __main__ 命名空间之外的其他地方(从函数或导入中)调用 cProfile.run() ,您也可能会遇到同样的错误。在这种情况下,您需要使用以下方法而不是 run() 方法:

cProfile.runctx("your code", globals(), locals())

Not the original poster's problem, but you can also get this same error if you are invoking cProfile.run() in something other than the __main__ namespace (from within a function or an import). In that case you need to use the following instead of the run() method:

cProfile.runctx("your code", globals(), locals())
维持三分热 2024-08-19 09:55:35

尽管 IPython 非常方便,但在很多罕见的情况下,它会破坏工作代码或掩盖错误。因此,当遇到此类神秘错误时,在标准解释器中尝试代码会很有用。

Although IPython is very handy, there is a lot of rare cases when it breaks working code or masks errors. So it's useful to try code in standard interpreter when you get such mystical errors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文