I need to have ten counters that I update in a loop if certain conditions are met.
My normal approach would be to create a list of 10 elements and update the elements of this list.
Dummy example:
my_counters = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
for i in range(100000):
if condition1:
my_counters[0] += 1
my_counters[4] += 1
elif condition2:
my_counters[2] += 1
my_counters[8] += 1
...
However this appears to be bad and inefficient code, which I don't really know why.
So I'd like to know what would be the best approach for this simple issue. Of course I have some suggestions:
1) Using one variable for each counter CONS: Too many variables, what if I need 100 counters?
2) Using a numpy array instead of a list (would then it be considered good and efficient code?)
3) Use a dictionary: This way each variable can have a representative name, but wouldn't the hash make the access slower than an index?
What is the correct way of solving this problem?
And also, why is updating elements of a list inneficient? Shouldn't the address be immediate to calculate? Base address + offset
Thanks