【问题标题】:How do I read the output of the IPython %prun (profiler) command?如何读取 IPython %prun (profiler) 命令的输出?
【发布时间】:2011-10-27 12:54:06
【问题描述】:

我运行这个:

In [303]: %prun my_function()
         384707 function calls (378009 primitive calls) in 83.116 CPU seconds

   Ordered by: internal time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    37706   41.693    0.001   41.693    0.001 {max}
    20039   36.000    0.002   36.000    0.002 {min}
    18835    1.848    0.000    2.208    0.000 helper.py:119(fftfreq)

--剪辑--

tottime、percall、cumtime 分别是什么? ncalls 相当明显(函数被调用的次数)。我的猜测是 tottime 是在函数中花费的总时间,不包括在它自己的函数调用中花费的时间; percall 是 ???; cumtime 是在函数调用中花费的总时间,包括在它自己的函数调用中花费的时间(当然,不包括重复计算)。 docs 不太有用;谷歌搜索也无济于事。

【问题讨论】:

    标签: python interpreter profiler ipython


    【解决方案1】:

    它只是 Python 自己的分析器的一个方便的包装器,其文档在这里:

    http://docs.python.org/library/profile.html#module-pstats

    引用:

    ncalls 对于通话次数,

    tottime 表示在给定函数中花费的总时间(不包括调用子函数的时间),

    percall是tottime除以ncalls的商

    cumtime 是在这个和所有子函数中花费的总时间(从调用到退出)。这个数字即使对于递归也是准确的 功能。

    percall 是 cumtime 除以原始调用的商

    【讨论】:

    • 一个只调用其他方法的方法怎么可能有显着的tottime