Re: List Performance



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
.



Relevant Pages

  • Re: Creating a Product/License Key
    ... Have the year from today date go through some algorithm (that can be ... reversed) that will append the date to the product key. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: How to rearrange text fields in sed?
    ... bugs where the expression didn't resolve to true in corner ... but at first sight it looks like it's an assignment. ... least what the algorithm is. ... newline, append " " otherwise. ...
    (comp.unix.questions)
  • Re: [OT] Looking for algorithm
    ... they possesed the same mark. ... I am looking for an algorithm to provide AI for a Tic Tac Toe (aka ... But not necessarily for Tic Tac ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: If I want to put an atom at the end of a list, which is better ?
    ... reorganize your algorithm so that the list traversals inherent in append ... the list that you want to append ... The end of a (proper) list is always NIL. ... possible to have a list ending in an atom, but then it is not a proper ...
    (comp.lang.lisp)
  • Re: Ranking Without Sorting
    ... >That approach still requires Oadditional storage for the mark bits. ... I don't know of any algorithm to invert a permutation ... Richard Harter, cri@tiac.net ...
    (comp.programming)