[Pyrex] Question, plus suggestions for improvement?

Giovanni Bajo rasky at develer.com
Tue Aug 1 21:40:19 UTC 2006


Josiah Carlson wrote:

>> If you need to perform many accesses, it's better to convert it to a
>> standard C array of integers/floats, allocated with malloc() (and
>> freed with free()). You can index it with any "cdef int" variable,
>> and this will provide C-like performance. This is especially useful
>> if the same data is used many times (eg: convert the data to C array
>> in the constructor, and then use it in all the methods).
>
> In my experience, malloc and free are horrible from a performance
> perspective.  The highest performing code I've produced never uses
> either of them (or really, never uses free).
>
> If one assumes a maximum fixed size of the input sequence, you can use
> stack allocation, which is both far faster, and impervious to "oops, I
> forgot to free" bugs.

Depends. What if you have an unbounded size, and it's often going to be
larger than Python standard pools so PyMem_Malloc won't help?

It also depends on how many times you access the list. If you do it many
times, it's much better to pay for the penalty of malloc once, rather than
going through PyList_GetItem + PyFloat_AsFloat thousands of times.
-- 
Giovanni Bajo




More information about the Pyrex mailing list