2012-11-14 33 views
4

我对蟒蛇的工作和整个寻找代码如何找到代码的统计和执行时间在Python

假设的统计和执行时间的一些概念来我有下面的代码

from time import gmtime, strftime 
import timeit 


def calculation(): 
    a = 2 
    b = 3 
    res = a + b 
    return res 

if 'name' == 'main' : 
    exec_time = timeit.timeit(calculation) 
    print exec_time 

结果:

0.2561519145965576 
从上面的代码,我能够找到的代码的执行时间

如此,但如何找到python中的代码统计信息?

最后我的本意是低于点

  1. 如何找到的代码在python
  2. 统计如何找到整个代码的执行时间在Python
  3. 的真正意思是什么统计代码?

被修改代码:

例如我有文件test.py

现在,在上面的代码我已经运行带命令上述文件下面

python -m cProfile test.py 

结果:

sh-4.2$ python -m cProfile test.py 
     4 function calls in 0.001 seconds 

    Ordered by: standard name 

    ncalls tottime percall cumtime percall filename:lineno(function) 
     1 0.001 0.001 0.001 0.001 test.py:1(<module>) 
     1 0.000 0.000 0.000 0.000 timeit.py:105(Timer) 
     1 0.001 0.001 0.001 0.001 timeit.py:53(<module>) 
     1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} 

所以我需要这样的东西,当我运行上面的代码时,我正在尝试的是将此功能打印到文件test.py内的统计信息中,而不是从终端运行命令python -m cProfile test.py

至少我想在文件运行时找到函数calculation()的统计信息和执行时间,因为实际上函数计算具有执行某些操作的大功能。

+1

“代码统计”是什么意思? – Chris

+2

您可能需要_profiling_(这是进一步研究的关键字)。另请参阅[文档](http://docs.python.org/2/library/profile.html)和[此问题](http://stackoverflow.com/q/3378953/1258041)。 –

+0

@chris:多数民众赞成我的怀疑实际上,我不知道为什么它给出的投票,我对此有疑问和接近SO –

回答

1

看来你要问的是如何为timeit模块编程接口。这是记录here。您需要一个样本集来计算统计信息,例如最小值,最大值,平均值等,这意味着运行时间模块中包含的Timeit类的重复方法可以计算多次。

例如:

timer = timeit.Timer(calculation) 
results = timer.timeit(10000) 
+0

其实我们可以找到执行时间通过传递Timer/timeit中的函数calucation()是否正确?很好,但统计数据如我需要上述所有结果信息,正如我在上面编辑的代码中提到的。也就是说,如果我们将该文件作为“python -m cProfile test.py”运行,我们将获得如上所述的全部输出(多少次函数调用等),但我不想用此命令运行该文件,而是当我运行文件为“python test.py”时,它应该显示上面的所有结果,那就是我们需要在test.py文件中实现cprofile功能 –

+0

对于你的问题是的。在我的例子中,上面的结果包含了所有10000个时序的列表。打印输出中的第一列是总的调用,在我的示例中为10000,因为这是传入的内容。第二列是总时间,这是列表中值的总和,以此类推。 –

0

我想你是问如何使用cProfile从代码中。事实证明,这很容易。 cProfile.Profile有两个未公开的方法,enabledisable,可用于启动和停止分析器。这意味着您可以轻松创建上下文管理器或装饰器。以下配方在一个对象中实现了这两个功能,并且包含一种使用pstat模块处理和打印输出的方式。

import cProfile, pstats, functools 

class profile: 

    def __init__(self, filename=None): 
     """ 
     If defined, output is written to *filename*, otherwise it 
     is processed using *pstats* and printed to STDOUT. 
     """ 
     self._filename = filename 
     self._prof = cProfile.Profile() 

    def __enter__(self): 
     self._prof.enable() 

    def __exit__(self, exc_type, exc_value, traceback): 
     self._prof.disable() 
     if self._filename is not None: 
      self._prof.dump_stats(self._filename) 
     else: 
      stats = pstats.Stats(self._prof) 
      self.print_stats(stats) 

    def print_stats(self, stats): 
     """ 
     This method processes the stats and prints them. 
     """ 
     stats.strip_dirs().sort_stats('cumulative').print_stats(20) 

    def __call__(self, func): 
     self._func = func 
     @functools.wraps(func) 
     def wrapper(*args, **kwargs): 
      with self: 
       return func(*args, **kwargs) 
     return wrapper 

所以用法是:

@profile() 
def calculation(): 
    a = 2 
    b = 3 
    res = a + b 
    return res 

calculation() 

with profile('output_file.pstat'): 
    calculation() 

您可以根据需要更改print_stats显示您需要的输出。