0

please, I need to help :) I have a longer cycle in python and I want measure total time in cycle every 1000 iterations and also time between last 1000 iterations (I mean difference between 1000 and 2000 iterations in cycle).

Variable which measuring total time from start I'm calling delta and time between 1000x iterations calling d2

start = time.time()
for i in range (0, iterations):
# ... logic in cycle ...
# calculating delta in each iteration
delta = time.time() - start
# Measuring time after every 1000 iterations
if (i + 1) % 1000 == 0:
    # Calculating  d2 time
    d2 = time.time() - start - d2
    print (str(i + 1) + ' items in ' + time.strftime("%H:%M:%S", time.gmtime(delta)) + ' (' + time.strftime("%H:%M:%S", time.gmtime(d2)) + ')' )

# After cycle
end = time.time() - start
print ('Total time: ' + time.strftime("%H:%M:%S", time.gmtime(end)))

Here are the results:

1000 items in 00:00:07 (00:00:07) 2000 items in 00:00:13 (00:00:06) 3000 items in 00:00:20 (00:00:13) 4000 items in 00:00:27 (00:00:13) 5000 items in 00:00:34 (00:00:20) 6000 items in 00:00:41 (00:00:20) 7000 items in 00:00:48 (00:00:27) 8000 items in 00:00:55 (00:00:27) 9000 items in 00:01:01 (00:00:34) 10000 items in 00:01:08 (00:00:34) 11000 items in 00:01:15 (00:00:41) 12000 items in 00:01:22 (00:00:41)

It seems that in first values it's OK (delta bewteen 7 seconds and 13 seconds in second line it's OK) but later this time is dissipating and confusing. Look at the last 2 lines. Delta between 00:01:08 and 00:01:15 is around 7 seconds (like in first 2 lines) and not 41 secods. Also the last line...

Where can I have a problem? Any idea please?

I'll be glad to any tip or help. Thank you very much

Regards

1 Answer 1

2

You have to update your d2 also with each 1000th cycle. I have named the old delta as old_delta for clarity.

import time

iterations = 5000
start = time.time()

old_delta = 0
for i in range(0, iterations):
    time.sleep(.002)
    # ... logic in cycle ...
    # calculating delta in each iteration
    delta = time.time() - start
    # Measuring time after every 1000 iterations
    if (i + 1) % 1000 == 0:
        # Calculating  d2 time
        d2 = delta - old_delta
        print(str(i + 1) + ' items in ' + time.strftime("%H:%M:%S", 
    time.gmtime(delta)) + ' (' + time.strftime("%H:%M:%S", time.gmtime(d2)) 
    + ')' )
        old_delta = delta


# After cycle
end = time.time() - start
print ('Total time: ' + time.strftime("%H:%M:%S", time.gmtime(end)))
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.