use*_*289 5 python profile performance time cprofile
嗨专家Python教徒,我开始使用cProfile,以便在我的程序上有更详细的计时信息.然而,令我非常不安的是,这是一个巨大的开销.知道为什么cProfile报告7秒,而时间模块只在下面的代码中报告2秒?
# a simple function
def f(a, b):
c = a+b
# a simple loop
def loop():
for i in xrange(10000000):
f(1,2)
# timing using time module
# 2 seconds on my computer
from time import time
x = time()
loop()
y = time()
print 'Time taken %.3f s.' % (y-x)
# timing using cProfile
# 7 seconds on my computer
import cProfile
cProfile.runctx('loop()', globals(), locals())
Run Code Online (Sandbox Code Playgroud)
因为它做了很多工作? time只需整个操作的时间,cProfile然后在仪器下运行它,这样就可以得到详细的细分.显然,分析并不意味着用于生产,因此2.5倍的开销似乎是一个很小的代价.
| 归档时间: |
|
| 查看次数: |
1903 次 |
| 最近记录: |