nem.ec

Programming Information Security Recipes Personal Code Snippets

Create a quick progress counter to monitor the performance of tight loops

Count the number of seconds between N iterations of a long loop

from datetime import time
import time

last_ts = time.time()
iterations_threshold = 1000000
num_iterations = 0
for item in ...:
    num_iterations += 1
    if num_iterations % iterations_threshold == 0:
        curr_ts = time.time()
        print(f'{datetime.now():%Y-%m-%d %H:%M:%S} '
              f'({int(curr_ts-last_ts)}) {num_iterations}')
        last_ts = curr_ts
Copied!

Use your own input

If you have values you'd like to use instead of the provided variables, type or paste your data into the variable inputs below. If Javascript is enabled, your text will automatically be added to the command and you can click to copy.