On Fri, Jun 21, 2013 at 3:46 AM, Daπid <
david...@gmail.com> wrote:
>> Exactly. Every numpy function is a Python function, so it is executed
>> dynamically and will add a lot of overhead within loops. For faster math
>> functions within loops, you should use the c versions available within
>> libc.math
>
> I always thought that was what cimport numpy was for, but it looks I
> was mistaken.
"cimport numpy" imports the numpy.pxd file -- declarations of numpy
functions, contants, what have you. This lets you use cython to do
_some_numpy stuff with direct C calls, etc.
> So, there is no way one could include numpy calls in the
> cython layer?
of course you can -- but they will still be pyton function calls, just
like most of Cython code.
IN theory, you could set things up so that cython could call, for
instance, np.max() directly without the python function caling
overhead, but chances are that would buy you nothing significant --
it's the work being done in that function that takes the time, not the
call itself (for any but trivially small arrays).
As for the OP's case, we haven't seen the code, but I suspect Sturla
is right -- np.max() takes longer than a cython loop that does the
same thing because np.max() is written to accommodate the general case
of strided nd-arrays -- a little more overhead in the iteration
through all the elements, and since the actual comparison is trivial,
the iteration overhead is significant.
-Chris
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R
(206) 526-6959 voice
7600 Sand Point Way NE
(206) 526-6329 fax
Seattle, WA 98115
(206) 526-6317 main reception
Chris....@noaa.gov