Skipping on memory in Python classes

the_rev_dharma_roadkill doug.hendricks at tnzi.com
Fri Aug 8 00:12:39 CEST 2003


"Raymond Hettinger" <vze4rx4y at verizon.net> wrote in message news:<hVkYa.15407$W%3.182 at nwrdny01.gnilink.net>...

[snip]

> >
> > Any other proven techniques out there?  Is there much point in
> > creating a new metaclass for my class?  How about replacing
> > emptyStrings with Nones?  Is there a fast (runtime) way of translating
> > between '' and None?
> 
> If the list of 200 elements doesn't change, it may be better to use a
> tuple instead of a list.

That sounds good, but it doesn't seem to make much difference.

> 
> If the list contents are all of the same type, the array module provides
> a space efficient storage solution.

Most of the elements are either empty strings or strings of len() from
1 to about 50.  As I read/experiment, array.array is good for elements
of
len() == 1. Very efficient, yes, but not flexible enough.

Maybe my own module, written in C, is the answer.  Or not.  My tests
seem to indicate that most of the problem is the per-object memory,
not the
list-size-related memory.  If I cut the size of the list from 200
elements down to 35 elements, my memory use is only cut in half. 
That's nothing to sneeze at, but it's obvious that other terms are
important.

Maybe not storing 100,000 instances as objects is the answer.  It
should be possible to represent each object as a single (large) tuple
or list or list of lists.  Code readability will suffer.  I'll
experiment at a later date.

> 
> Empty strings are like None in that they all refer to a single object,
> so there are no savings there.

That is good to know.  Thanks.

> 
> 
> Raymond Hettinger

Doug




More information about the Python-list mailing list