I need to get measurements every 500 iterations in merge sorting algorithm. I've been trying to make it using Python's time module but it doesn't work well.
import random
import time
start_time = time.time()
count = [1]
def mergesort(A):
if len(A) > 1:
m = len(A)//2
l = A[:m]
r = A[m:]
mergesort(l)
mergesort(r)
i = j = k = 0
count.append(1)
while i < len(l) and j < len(r):
if l[i] < r[j]:
A[k] = l[i]
i += 1
else:
A[k] = r[j]
j += 1
k += 1
while i < len(l):
A[k] = l[i]
i += 1
k += 1
while j < len(r):
A[k] = r[j]
j += 1
k += 1
if sum(count) >= 500 and sum(count) % 500 == 0:
print(sum(count))
print(time.time() - start_time)
A = random.sample(range(0, 99999), 11000)
mergesort(A)
print(A)
Instead, it prints the time sometimes multiple times and some skips. What could I do to make it actually work?