【发布时间】:2012-11-02 16:13:53
【问题描述】:
我正在研究 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() 的统计信息和执行时间,因为实际上函数计算具有执行某些操作的大功能。
【问题讨论】:
-
“代码统计”是什么意思?
-
您可能需要profiling(这是进一步研究的关键词)。另请查看the docs 和this question。
-
@chris:这实际上是我的疑问,我不知道为什么它被否决了,我对此表示怀疑并接近了 SO
-
我编辑了我的代码,任何人都可以在上面查看....
标签: python statistics execution-time