I am creating a class, among other things, a dictionary with integer keys and list values. Adding values โโto this dictionary seems to be a real bottleneck, and I was wondering if there could be some way to speed up my code.
class myClass():
def __init__(self):
self.d = defaultdict(list)
def addValue(self, index, value):
self.d[index].append(value)
Is this really the best way to do this? I don't care about the order of the values, so maybe there is a more suitable data structure with faster additions. Again, "append" is apparently not the main problem, because if I just add to the empty list, the code is much faster. I assume loading a previously saved list takes most of the time?
I found out that the problem is not in the dict, but in the append list (although I stated differently in my original post, for which I apologize). This issue is due to a bug in the Python garbage collector, which is well explained in this other question . Disabling gc before adding all values, and then re-enabling, speeding up the process is very important!
source
share