Python, calculating the status of computation slows down the computation itself

Viewed 113

This is a basic example of what I'm talking about:

Count from 0 to 10000000

import time

k = 0

beginning = time.time()

while k < 10000000:

    k = k+1

elapsed = round(time.time()-beginning, 5)

print (elapsed)

Now with a sort of "status" of the computation (displays percentage every 1 second):

import time

k = 0

beginning = time.time()
interval = time.time()

while k < 10000000:

    if time.time() - interval > 1:
        print (round((k/100000), 5))
        interval = time.time()

    k = k+1

elapsed = round(time.time()-beginning, 5)

print (elapsed)

The first example takes 3.67188 seconds, the second example takes 12.62541 seconds. I guess that's because the scripts has to continuously check if 1 second has elapsed. Is there a way to solve this issue? I've found something about threads and multiprocess but i can't figure out how to implement it. Thanks

2 Answers
Related