List Performance
Maric Michaud
maric at aristote.info
Mon Jun 30 18:21:28 EDT 2008
Le Monday 30 June 2008 22:21:35 Terry Reedy, vous avez écrit :
> > Well, as I posted few days ago, one could envisage, as a pure python
> > optimization for dealing with long list, to replace an algorithm with a
> > lot of append by something like this :
> >
> > mark = object()
> >
> > datas = [ mark ] * expected_size
>
> datas = [None] * expected_size
> has been a standard idiom since before object() existed ;-)
> and works fine *unless* one wants to add None explicitly
> and have that be different from 'unused'.
Yes, in fact I used a marker because it I thought of it primarily as outbound
for the list (like \0 for strings in C), but it doesnt' matter what is the
object you put in the list, if you know at every moment its size.
A subclass of list will indeed have to override most of the methods of its
parent (not just some as I assumed before), using extend for reallocation
with some sort of iterator with size, as it work in my previous example with
xrange, something like that :
>>>[31]: class iter_with_len(object) :
def __init__(self, size, obj=None) :
self.size = size
self.obj = obj
def __len__(self) : return self.size
def __iter__(self) : return itertools.repeat(self.obj, len(self))
....:
....:
--
_____________
Maric Michaud
More information about the Python-list
mailing list