使用 cProfile 分析 Python 中类的方法?

发布于 2024-10-08 22:21:07 字数 217 浏览 2 评论 0原文

我想使用 cProfile 来分析 Python 中函数的方法。我尝试了以下方法:

import cProfile as profile

# Inside the class method...
profile.run("self.myMethod()", "output_file")

但它不起作用。如何使用“run”调用 self.method?

I'd like to profile a method of a function in Python, using cProfile. I tried the following:

import cProfile as profile

# Inside the class method...
profile.run("self.myMethod()", "output_file")

But it does not work. How can I call a self.method with "run"?

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

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

发布评论

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

评论(5

煮茶煮酒煮时光 2024-10-15 22:21:07

run 只是尝试exec 您传递给它的字符串。如果 self 未绑定到您正在使用的探查器范围内的任何内容,则您无法在 run 中使用它!使用runctx方法传入调用探查器范围内的本地和全局变量:

>>> import time
>>> import cProfile as profile
>>> class Foo(object):
...     def bar(self):
...             profile.runctx('self.baz()', globals(), locals())
...
...     def baz(self):
...             time.sleep(1)
...             print 'slept'
...             time.sleep(2)
...
>>> foo = Foo()
>>> foo.bar()
slept
         5 function calls in 2.999 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    2.999    2.999 <stdin>:5(baz)
        1    0.000    0.000    2.999    2.999 <string>:1(<module>)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        2    2.999    1.499    2.999    1.499 {time.sleep}

请注意最后一行:time.sleep 是占用时间的内容。

run just tries to exec the string you pass it. If self isn't bound to anything in the scope of the profiler you are using, you can't use it in run! Use the runctx method to pass in the local and global variables in the scope of the call to the profiler:

>>> import time
>>> import cProfile as profile
>>> class Foo(object):
...     def bar(self):
...             profile.runctx('self.baz()', globals(), locals())
...
...     def baz(self):
...             time.sleep(1)
...             print 'slept'
...             time.sleep(2)
...
>>> foo = Foo()
>>> foo.bar()
slept
         5 function calls in 2.999 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    2.999    2.999 <stdin>:5(baz)
        1    0.000    0.000    2.999    2.999 <string>:1(<module>)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        2    2.999    1.499    2.999    1.499 {time.sleep}

Notice the last line: time.sleep is what's taking up the time.

不必了 2024-10-15 22:21:07

使用 profilehooks 装饰器

http://pypi.python.org/pypi/profilehooks

Use the profilehooks decorator

http://pypi.python.org/pypi/profilehooks

苦行僧 2024-10-15 22:21:07
  import cProfile
  p = cProfile.Profile()
  p.runcall(self.myMethod)
  p.print_stats()

Profile 类记录在此处

  import cProfile
  p = cProfile.Profile()
  p.runcall(self.myMethod)
  p.print_stats()

The Profile class is documented here.

鸢与 2024-10-15 22:21:07

如果您的配置文件下的函数返回值,您需要稍微更改@katrielalex 的优秀答案:

...             profile.runctx('val = self.baz()', globals(), locals())
...             print locals()['val']

If your function under profile returns value(s), you need to change the excellent answer from @katrielalex slightly:

...             profile.runctx('val = self.baz()', globals(), locals())
...             print locals()['val']
海未深 2024-10-15 22:21:07

我不建议对单个例程进行分析,因为这意味着提前知道那里存在问题。

性能问题的一个基本方面是它们是偷偷摸摸的。
它们并不在你想象的地方,因为如果它们在,你早就解决了它们。

最好以实际工作负载运行整个程序,并让分析技术告诉您问题出在哪里。

这是一个示例,其中分析发现了问题,并且它不是预期的地方。

I wouldn't recommend profiling a single routine, because that implies knowing in advance there's a problem there.

A fundamental aspect of performance problems is they're sneaky.
They're not where you think they are, because if they were you would have solved them already.

It's better to run the whole program with a realistic workload and let the profiling technique tell you where the problems are.

Here's an example where profiling finds the problem, and it is not where expected.

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