Why doesn't it just take the same amount of time? Does it have something to do with other programs, background processes etc?
import time
def calcProd():
'Calculates the product of numbers from 1 through 100,000'
product = 1
for x in range(1, 100000):
product *= x
return product
start = time.time()
calcProd()
end = time.time()
elapsed = round((end - start), 7)
print(elapsed)
and the times(in seconds) tested are as follows:
24.1244698
22.9401324
23.3407295
23.2885954
23.3982868
21.177716
I know this is may seem to be a trivial question, but bear with me. And is there a more efficient way to perform the same function? Thanks
*The primary part of this question was to spark discussion as to why the times vary so much when running the same program over and over. The last part was more of just a spontaneous question.