有没有可以分析Python代码的工具?

发布于 2024-11-14 02:28:38 字数 125 浏览 6 评论 0原文

我正在寻找一个工具来分析我的 python 脚本。例如

  1. 哪部分代码花费的时间最多,
  2. 哪部分代码消耗太多内存
  3. 等等......

有类似的东西吗?

I am looking for a tool to analyze my python script. For example

  1. which part of the code takes the most time
  2. which part of the code consumes too much memory
  3. and so on...

Is there something like that?

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

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

发布评论

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

评论(3

余生共白头 2024-11-21 02:28:38

查看cProfile。这是一个使用示例:

me@mine:~ $ cat foo.py 
def double(i):
    return i * 2

def halve(i):
    return i / 2.0

for i in range(10000):
    double(i)
    halve(i)
me@mine:~ $ python -m cProfile foo.py 
         20005 function calls in 0.009 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.009    0.009 <string>:1(<module>)
        1    0.006    0.006    0.009    0.009 foo.py:1(<module>)
    10000    0.001    0.000    0.001    0.000 foo.py:1(double)
    10000    0.002    0.000    0.002    0.000 foo.py:4(halve)
        1    0.000    0.000    0.009    0.009 {execfile}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        1    0.000    0.000    0.000    0.000 {range}

一个好的内存分析器,如 Sven MarnachHeapy

Take a look at cProfile. Here's a usage example:

me@mine:~ $ cat foo.py 
def double(i):
    return i * 2

def halve(i):
    return i / 2.0

for i in range(10000):
    double(i)
    halve(i)
me@mine:~ $ python -m cProfile foo.py 
         20005 function calls in 0.009 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.009    0.009 <string>:1(<module>)
        1    0.006    0.006    0.009    0.009 foo.py:1(<module>)
    10000    0.001    0.000    0.001    0.000 foo.py:1(double)
    10000    0.002    0.000    0.002    0.000 foo.py:4(halve)
        1    0.000    0.000    0.009    0.009 {execfile}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        1    0.000    0.000    0.000    0.000 {range}

A good memory profiler, as mentioned in the post linked to by Sven Marnach, is Heapy

逐鹿 2024-11-21 02:28:38

我经常使用 Ipython 来分析我的代码。像这样使用神奇命令“%run”执行脚本(在 ipython 提示符内):

    %run -p your_python_script.py

在 Python 分析器模块的控制下运行程序。

您甚至可以使用“%prun”魔法分析语句(例如函数调用):

    %prun a_python_statement

%prun 的优点在于它在当前会话的上下文中执行语句(即您可以使用您之前定义的变量以及当前命名空间中的任何其他内容)。

如果您想获取每行分析信息,我发现 line_profiler 模块非常方便。它有点旧,但它完成了工作......正如 Robert Kern 编码的内容所期望的那样;-)。

I frequently use Ipython to profile my code. Executing a script with the magic command "%run" like this (inside an ipython prompt):

    %run -p your_python_script.py

runs the program under the control of the Python profiler module.

You can even profile statements (e.g. a function call) with the "%prun" magic:

    %prun a_python_statement

What is nice about %prun, is that it executes the statement in the context of your current session (i.e. you can use variables you previously defined and anything else in your current namespace).

If you want to get per-line profiling information, I found the line_profiler module to be quite handy. It is a bit old, but it does the job... as expected from something coded by Robert Kern ;-).

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