I have a collections.Counter() object that keeps getting added Counter objects (accumulated) in a loop. As the loops pass and the accumulated Counter increases (more entries), the accumulate (+=) operation becomes slower.
A workaround is to use the Counter in batches and accumulate partial counters to add (reduce) them all in the end. But I would like to know why this is happening (maybe the underlying implementation uses hashmaps and the bucket size is not dynamic therefore collisions happen more and more often?)
cnt = Counter()
for i in range(len(list_files_txt)):
t0 = time()
f = list_files_txt[i]
print('[{}/{}]'.format(i, len(list_files_txt)))
with open(f, 'r') as txt_f:
cnt += Counter(txt_f.read().lower().replace('\n', ' ').split(' '))
d_t = time() - t0
print('Time: ', d_t)
with open('times.txt', 'a') as times_f:
times_f.write(str(d_t)+'\n')
Expected results: The printed times are constant-ish in the entire loop
Actual results: The printed times increase as the loops goes forward
Actual results (code execution):
[0/185187]
Time: 0.0009126663208007812
[1/185187]
Time: 0.0011148452758789062
[2/185187]
Time: 0.0006835460662841797
[3/185187]
Time: 0.0009150505065917969
[4/185187]
Time: 0.0005855560302734375
# A few thousand iterations later...
[14268/185187]
Time: 0.1499614715576172
[14269/185187]
Time: 0.14177680015563965
[14270/185187]
Time: 0.1480724811553955
[14271/185187]
Time: 0.14731359481811523
[14272/185187]
Time: 0.15594100952148438
Here is a plot that demonstrates the tendency:
