[Pyrex] Question, plus suggestions for improvement?
Josiah Carlson
jcarlson at uci.edu
Tue Aug 1 21:14:54 UTC 2006
"Giovanni Bajo" <rasky at develer.com> wrote:
> Bill Mill wrote:
>
> > 1) I see very few native C operations in the source code, just
> > eyeballing it. How could I restructure my code to bring more in?
>
> Use more native types.
>
> > 2) Could I make list access faster? Optionally convert the list into
> > an array of (integers, characters, strings, etc) by a user's argument?
>
> 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.
- Josiah
More information about the Pyrex
mailing list