こんにちは、専門家の Pythonists です。プログラムのより詳細なタイミング情報を取得するために、cProfile を使用し始めています。ただし、かなりのオーバーヘッドがあることは非常に気がかりです。以下のコードで、time モジュールが 2 秒しか報告していないのに、cProfile が 7 秒を報告した理由は何ですか?
# 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())