This part of my code does not scale if dimension gets bigger.
I loop over my data and accumulate them every dt time window. To do this I compare lower and upper time value. When I reach upper bound, I break the for loop for efficiency. The next time I run for loop I want to start not from its beginning but from the element I stopped previously, for efficiency. How can I do that?
I tried to remove/pop elements of the list but indexes get messed up. I read that I cannot modify the list I loop over, but my goal seems to be not uncommon so there has to be solution. I don't care about original data list later in my code, I only want optimization of my accumulation.
# Here I generate data for you to show my problem
from random import randint
import numpy as np
dimension = 200
times = [randint(0, 1000) for p in range(0, dimension)]
times.sort()
values = [randint(0, dimension) for p in range(0, dimension)]
data = [(values[k], times[k]) for k in range(dimension)]
dt = 50.0
t = min(times)
pixels = []
timestamps = []
# this is my problem
while (t <= max(times)):
accumulator = np.zeros(dimension)
for idx, content in enumerate(data):
# comparing lower bound of the 'time' window
if content[1] >= t:
# comparing upper bound of the 'time' window
if (content[1] < t + dt):
accumulator[content[0]] += 1
# if I pop the first element from the list after accumulating, indexes are screwed when looping further
# data.pop(0)
else:
# all further entries are bigger because they are sorted
break
pixels.append(accumulator)
timestamps.append(t)
t += dt