Re: List Performance





Maric Michaud wrote:
Le Monday 30 June 2008 15:52:56 Gerhard Häring, vous avez écrit :
Larry Bates wrote:

If, on the other hand, we knew beforehand how big the list will get
approximately, we could avoid all these reallocations. No problem with
Python's C API:

PyAPI_FUNC(PyObject *) PyList_New(Py_ssize_t size);

But you can't do it directly from Python, unless you (ab)use ctypes.

-- Gerhard

--
http://mail.python.org/mailman/listinfo/python-list

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'.


# working with the datas while maintaining the effective currrently used size

Of course one could even subclass list and redefine __len__, append, and some other methods to deal with this "allocated by block" list.

An interesting idea if one does this at least a few times and wants to use .append and .extend instead of explicit indexing.

One could also make such a subclass a 'no-grow' list if appropriate (when an attempt to grow it would indicate a bug).

tjr

.



Relevant Pages

  • Re: Accessors in Python (getters and setters)
    ... used it in 27000 lines of Python code (which certainly took some time to ... Naming attributes and naming methods are entirely different ... operator only to callables, but this doesn't make such a difference ... But it's *your* responsability to choose good names for the API.> ...
    (comp.lang.python)
  • Re: zlib interface semi-broken
    ... implement one-shot de/compression of strings. ... functions that do one-shot compression and decompression. ... That file interface could form a third API, ... conform to what python expects of files. ...
    (comp.lang.python)
  • Re: Accessors in Python (getters and setters)
    ... used it in 27000 lines of Python code (which certainly took some time to ... develops might misinterpret exactly what "tmp" does. ... Failing to understand a language semantics ... But it's *your* responsability to choose good names for the API.> ...
    (comp.lang.python)
  • Re: Whats so funny? WAS Re: rotor replacement
    ... I don't disagree about the API. ... AES module (or rather, a generic block cipher module with AES and DES) ... if I want ECC in a Python ...
    (comp.lang.python)
  • Re: Reading a file that is changing and getting the new lines
    ... As the python script is running a user/application ... Is this an independent process, that appends data onto the same file, after the python script has opened it, but while it's still running? ... the other application simultaneously tries to append. ... I suspect the sharing problems aren't going to be portable between operating systems, so you might want to answer the usual "what version of Python, on what OS" question that we seem to have for most new questions. ...
    (comp.lang.python)