Hoyt Koepke wrote:
>> Just a question: Is there a reason you like to keep numpy in a "cnp"
>> prefix (I'm assuming you keep plain numpy in "np"). I'm asking because
>> if there is a reason I'm interested in eliminating it.
>
> Perhaps just me misunderstanding things. I guess I was assuming that
>
> import numpy as np
>
> and
>
> cimport numpy as cnp
>
> pulled two different things, one being the numpy.pxd interface (or
> something like that) and the other being the one I'm used to in
> python. But now that you mention it, it doesn't make any sense that
> they are different so I'm not sure how I arrived at that conclusion.
No, you are right :-) cimport pulls in numpy.pxd (from Cython/Includes)
and is "something else" entirely.
However we hope to make this increasingly transparent, as there's been
talk that at some point in the future the "import" statement can start
to mean both things at once automatically and then numerical users
wouldn't need to learn about cimport. And so I wanted to know your
reasons (thanks!). Doing what you do is perfectly ok.
>> As for efficiency, example 1 (not allowed) and 2 are the same except for
>> refcounting (which doesn't matter at all in a function using a buffer),
>> so use 2.
>
> I guess this implies that creating a buffer from a python object is
> quite efficient. Is that correct?
Looking it up in my stomach feeling, it is something like a function
dispatch to a function stored in a pointer + 2-3 normal function calls +
about 15 assignments and 7 comparisons (or something in that area). No
heap allocations.
So if your function is doing some calculation for reasonably large n you
won't notice. It is probably less than the overhead of a call to any
regular numpy library functions. However something like this:
cdef swap_array_elem(ndarray[int, 2] buf, int x1, int y1, int x2, int y2):
buf[x1, y1], buf[x2, y2] = buf[x2, y2], buf[x1, y1]
and using it in your inner loop is still out of the question.
--
Dag Sverre
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev