Automatic Generation of Python Class Files

Hrvoje Niksic hniksic at xemacs.org
Tue Oct 30 05:24:05 EDT 2007


Fuzzyman <fuzzyman at gmail.com> writes:

> We recently had to change an object pipeline from new style classes
> to old style. A lot of these objects were being created and the
> *extra overhead* of new style classes was killing us. :-)

Can you please expand on this?  What extra overhead of new-style
classes are you referring to?

Memory overhead seems to firmly favor new-style classes, especially
for small object.  A trivial small-object allocation test such as

# After running this, use "top" or "ps" to see the ballpark memory
# consumption.  Remove "(object)" to measure for old-style class.
python -c 'import time
class A(object): pass
l=[A() for n in xrange(2000000)]
time.sleep(100)'

shows the Python process growing to 78M with a new-style class and to
354M with an old-style class.  And that is without even starting to
use actual optimizations, such as using __slots__!  Instantiation time
difference is less drastic, but still shows significant improvement
with the use of new-style classes: 0.27 usec for new-style, 0.40 usec
old-style (measured with timeit, subtracted baseline overhead).



More information about the Python-list mailing list