[Pyrex] Question about +=, -=, *=, etc.
Phillip J. Eby
pje at telecommunity.com
Thu Jun 3 19:48:27 CEST 2004
At 10:00 AM 6/3/04 -0600, Fernando Perez wrote:
>I'd really like to use Pyrex for my
>code, especially now that I see there are good examples of how to deal with
>Numeric arrays.
>
>However, the critical path of my codes relies heavily on things like:
>
>for i in long_list:
> my_array += some_function(i)
>
>For code like this, which is performance-critical, pyrex sounds great. I
>can't write the whole thing in Fortran, because there's key reliance on python
>dicts for many things in there.
Try this as a simple workaround:
cdef extern from "Python.h":
object PyNumber_InPlaceAdd(object o1, object o2)
for i in long_list:
my_array = PyNumber_InPlaceAdd(my_array,some_function(i))
This is effectively the code that a += operator would generate.
In general, if you're looking to improve Python performance via Pyrex, it's
important to know what functions are available in the Python/C API, as
calling these functions directly can, under the right circumstances, yield
further performance boosts over "naive" Pyrex code.
More information about the Pyrex
mailing list