0

我是 cProfile 的新手。

我在我的程序上运行了 cProfile,它吐出了这个:

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1  290.732  290.732  313.069  313.069 newad.py:1(<module>)

newad.py 的第一行是:

1  from datetime import date

这条线应该花这么多时间吗?我能做些什么呢?

4

1 回答 1

3

cProfile 只是向您显示在该模块中花费的时间。行号似乎只是表示该模块中处理的第一条语句 - 因为您有一个多行文档字符串,它显示了文档字符串的最后一行。

"""
Test module for cProfile stats

"""



import time

def wait(t):
    time.sleep(t)

wait(5)

给出:

   $ python -m cProfile test.py
         4 function calls in 5.002 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    5.001    5.001 test.py:10(wait)
        1    0.001    0.001    5.002    5.002 test.py:4(<module>)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        1    5.001    5.001    5.001    5.001 {time.sleep}

请注意,第一行显示在函数中花费的时间,wait而第二行显示在模块中花费的时间。

于 2014-04-01T22:23:58.590 回答