I am a beginner just starting to profile my code and was confused why the elapsed time given by cProfile was so off from the time given by using time.time().
# Python 2.7.2
import cProfile
def f(n):
G = (i for i in xrange(n))
sum = 0
for i in G:
sum += i
num = 10**6
cProfile.run('f(num)')
This gives 1000004 function calls in 2.648 seconds
Yet with time.time(), I get 0.218000173569 seconds
import time
x = time.time()
f(num)
print time.time() - x
From what I have read, I guess this may be because of the overhead of cProfile. Are there any general tips for when cProfile timing is likely to be very off, or ways to get more accurate timing?