Re: [Cython] Lists?
On Sep 1, 2008, at 10:41 AM, Hoyt Koepke wrote:
> This is actually something I am wondering about as well, so I hope
> it's okay to join the discussion. My understanding is that when you
> declare a variable to have a list, tuple, or dict type, then cython
> optimizes it by using faster direct ways to accessing the elements
> when you use them. However, since these containers always store the
> elements as PyObject* pointers which hold any type, it's too difficult
> to use element type information in a robust way. Is that correct?
> What is the most "cythonic" way to mimic the list functionality when
> you do have homogeneous list content? I have run into cases in
> numerical calculations where I need to be append an arbitrary number
> of numbers to a list (e.g. accumulating data statistics while
> processing large amounts of data). In such contexts, using a list is
> too slow. One thing I've done is use a numpy array with a significant
> amount of extra space, keep a separate counter to denote the end, and
> access it through the nice buffer interface. This works pretty well,
> but seems quite clunky compared to how natural it is in python. Doing
> slicing and concatenation stuff would also be nice.
The traditional way to do this is the same as in C, with malloc,
realloc, etc. Then one can create actual int*, double*, etc. as in C.
Of course then one has to worry about memory leaks and/or
segfaults... The Numpy approach is a good one too, and there is a