您好 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())