weakref, memory management and execution slow down in PyQt4

Michael Torrie torriem at gmail.com
Tue Sep 9 17:32:19 CEST 2014

Reposting to list, instead of directly to kjs

On 09/08/2014 08:45 PM, kjs wrote:
> Thanks for the consideration Michael. If you do get the data, and are
> able to run the code, let me know if you notice anything interesting.

Yeah I don't think I'll be able to have the time to download a 3 GB file.

>> Is there a reason you are using setattr and getattr instead of a proper
>> data structure?  both of those calls are rather expensive.  Would
>> probably be cheaper to use some kind of array, dictionary, or other
>> purpose-built data structure?
> You're right, a dictionary can do everything I need and more. This
> happened to be the first thing I thought of, and I didn't imagine it
> would be very expensive. I figured it was simply a different way of
> defining and retrieving a class variable. IE setattr(self, foo, True) ==
> self.foo = True.

Yes you're correct.  It is the equivalent.  But it always involves
lookup in the object's dictionary, which is big O order O(n log n)
complexity for each and every access.  A list would be far faster,
essentially O(1) (I think?) after the single name lookup, since you can
access the elements by number.  Indexing into a list doesn't involve
doing name lookups; you just specify an offset.  No idea how much
faster, but significantly so.  If a list is too slow, there are other
array-like classes you can use (like numpy arrays) that do offer true
O(1) lookups.

Besides that, it's not typical use of setattr and getattr in python.

More information about the Python-list mailing list