It's ok to __slots__ for what they were intended

MrJean1 MrJean1 at gmail.com
Fri Dec 21 15:38:01 EST 2007


My milage does vary, see this older post

  <http://mail.python.org/pipermail/python-list/2004-May/261985.html>

Similar figures are shown with Python 2.5, both for 32- and 64-bit.

/Jean Brouwers


On Dec 21, 12:07 pm, Fredrik Lundh <fred... at pythonware.com> wrote:
> John Nagle wrote:
> >      I'd like to hear more about what kind of performance gain can be
> > obtained from "__slots__".  I'm looking into ways of speeding up
> > HTML parsing via BeautifulSoup.  If a significant speedup can be
> > obtained when navigating large trees of small objects, that's worth
> > quite a bit to me.
>
> The following micro-benchmarks are from Python 2.5 on a Core Duo
> machine.  C0 is an old-style class, C1 is a new-style class, C2 is a
> new-style class using __slots__:
>
> # read access
> $ timeit -s "import q; o = q.C0(); o.attrib = 1" "o.attrib"
> 10000000 loops, best of 3: 0.133 usec per loop
> $ timeit -s "import q; o = q.C1(); o.attrib = 1" "o.attrib"
> 10000000 loops, best of 3: 0.184 usec per loop
> $ timeit -s "import q; o = q.C2(); o.attrib = 1" "o.attrib"
> 10000000 loops, best of 3: 0.161 usec per loop
>
> # write access
> $ timeit -s "import q; o = q.C0(); o.attrib = 1" "o.attrib = 1"
> 10000000 loops, best of 3: 0.15 usec per loop
> $ timeit -s "import q; o = q.C1(); o.attrib = 1" "o.attrib = 1"
> 1000000 loops, best of 3: 0.217 usec per loop
> $ timeit -s "import q; o = q.C2(); o.attrib = 1" "o.attrib = 1"
> 1000000 loops, best of 3: 0.209 usec per loop
>
> $ more q.py
> class C0:
>      pass
>
> class C1(object):
>      pass
>
> class C2(object):
>      __slots__ = ["attrib"]
>
> Your mileage may vary.
>
>  > I'm looking into ways of speeding up HTML parsing via BeautifulSoup.
>
> The solution to that is spelled "lxml".
>
> </F>




More information about the Python-list mailing list