Time running program varying wildly on each try?

68 views Asked by At

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:

  1. 24.1244698

  2. 22.9401324

  3. 23.3407295

  4. 23.2885954

  5. 23.3982868

  6. 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.

0

There are 0 answers