我对蟒蛇的工作和整个寻找代码如何找到代码的统计和执行时间在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中的代码统计信息?
最后我的本意是低于点
- 如何找到的代码在python
- 统计如何找到整个代码的执行时间在Python
- 的真正意思是什么统计代码?
被修改代码:
例如我有文件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()
的统计信息和执行时间,因为实际上函数计算具有执行某些操作的大功能。
“代码统计”是什么意思? – Chris
您可能需要_profiling_(这是进一步研究的关键字)。另请参阅[文档](http://docs.python.org/2/library/profile.html)和[此问题](http://stackoverflow.com/q/3378953/1258041)。 –
@chris:多数民众赞成我的怀疑实际上,我不知道为什么它给出的投票,我对此有疑问和接近SO –