2014-04-01 122 views
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 

是这条线应该采取这种多少时间?我能做些什么呢?

+1

你是怎么运行cProfile的?您的模块是否包含除导入语句之外的任何内容? –

+0

@PeterGibson我再次运行cProfile。这一次它将进口陈述确定为花费最多的时间。我在编辑这个问题。 –

+0

如果这个结果被认为是“错误的”,你期望或希望取得的结果是什么? – Deestan

回答

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所花费的时间,而第二行显示模块中花费的时间。

+0

谢谢!我疯了,试图找出为什么这不起作用。现在优化模块内的代码... –