[Numpy-discussion] Returning numpy scalars in cython functions

Keith Goodman kwgoodman@gmail....
Thu Nov 18 12:14:59 CST 2010


On Thu, Nov 18, 2010 at 10:08 AM, Francesc Alted <faltet@pytables.org> wrote:
> A Thursday 18 November 2010 18:51:04 Keith Goodman escrigué:

>> What's the best way to make it return a numpy long int, or whatever
>> it is called, that has dtype, ndim, size, etc. class methods? The
>> only thing I could come up with is changing the last line to
>>
>>     return np.array(asum)[()]
>>
> Perhaps the scalar constructor is your best bet:
>
>>>> type(np.array(2)[()])
> <type 'numpy.int64'>
>>>> type(np.int_(2))
> <type 'numpy.int64'>
>>>> timeit np.array(2)[()]
> 1000000 loops, best of 3: 791 ns per loop
>>>> timeit np.int_(2)
> 1000000 loops, best of 3: 234 ns per loop

Perfect! Thank you.

>> a = np.arange(10)
>> timeit mysum2(a)
1000000 loops, best of 3: 1.16 us per loop
>> timeit mysum2_francesc(a)
1000000 loops, best of 3: 451 ns per loop

I also added @cython.wraparound(False).


More information about the NumPy-Discussion mailing list