为什么我的代码这么慢?

时间:2014-04-01 22:08:32

标签: python time cprofile

我是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

这条线应该花费这么多时间吗?我该怎么做呢?

1 个答案:

答案 0 :(得分: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中花费的时间,而第二行显示在模块中花费的时间。