[Cython] Cython syntax to pre-allocate lists for performance

Zaur Shibzukhov szport at gmail.com
Thu Mar 7 15:39:32 CET 2013


2013/3/7 Zaur Shibzukhov <szport at gmail.com>

> I guess the problem is to construct new (even empty) list with
> pre-allocated memory exactly for N elements.
>
> N*[NULL] - changes semantics because there can't be list with N elements
> and filled by NULL.
> N*[None] - more expansive for further assignments because of Py_DECREFs.
>
> I suppose that N*[] could do the trick. It could be optimized so that N*[]
> is equal to an empty list but with preallocated memory exactly for N
> elements. Could it be?
>
>
Cython optimize already PyList_Append very well. Theofore scenario when
first one create empty list with exactly prealocated memory for N elements
 and second eval elements of the list and add them using plain list.append
could optimized in Cython very well too. As result constructed list will
contain memory only for N elements. This allows don't vast memory when one
need to build many many lists with relative big size.

Zaur Shibzukhov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cython-devel/attachments/20130307/186df5cf/attachment-0001.html>


More information about the cython-devel mailing list