optimizing large dictionaries

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Thu Jan 15 17:58:01 EST 2009


On Thu, 15 Jan 2009 23:22:48 +0100, Christian Heimes wrote:

>> is there anything that can be done to speed up this simply code? right
>> now it is taking well over 15 minutes to process, on a 3 Ghz machine
>> with lots of RAM (though this is all taking CPU power, not RAM at this
>> point.)
> 
> class MyClass(object):
>     # a new style class with slots saves some memory 
>     __slots__ = ("field1", "field2", "field2")

I was curious whether using slots would speed up attribute access.

>>> class Parrot(object):
...     def __init__(self, a, b, c):
...             self.a = a
...             self.b = b
...             self.c = c
...
>>> class SlottedParrot(object):
...     __slots__ = 'a', 'b', 'c'
...     def __init__(self, a, b, c):
...             self.a = a
...             self.b = b
...             self.c = c
...
>>>
>>> p = Parrot(23, "something", [1, 2, 3])
>>> sp = SlottedParrot(23, "something", [1, 2, 3])
>>>
>>> from timeit import Timer
>>> setup = "from __main__ import p, sp"
>>> t1 = Timer('p.a, p.b, p.c', setup)
>>> t2 = Timer('sp.a, sp.b, sp.c', setup)
>>> min(t1.repeat())
0.83308887481689453
>>> min(t2.repeat())
0.62758088111877441


That's not a bad improvement. I knew that __slots__ was designed to 
reduce memory consumption, but I didn't realise they were faster as well.



-- 
Steven



More information about the Python-list mailing list