简体   繁体   中英

Cython: Effectively using Numpy in Pure Python Mode

I am fairly new to using Cython and I am interested in using the "Pure Python" mode.

The work that I am doing right now uses numpy extensively and knowing that there is a C api for numpy, I was excited to see what it could do.

As a small test, I put together two small test files, test.py and test.pxd . Their content is as follows:

test.py:

import cython
import numpy as np

@cython.locals(array=np.ndarray)
@cython.returns(np.ndarray)
def test(array):
    return np.cumsum(array)

test_array = np.array([1,2,3,4,5])
test(test_array)

test.pxd:

# cython: language_level=3
cimport numpy as np

cdef np.ndarray test(np.ndarray array)

I then compiled these files with cython -a test.py with the hopes that I would see little to no python interaction when calling np.cumsum(). However when I inspected the generated HTML file, I found the following:

在此处输入图片说明

From this, it appears that my call to np.cumsum heavily interacts with python, which is something that feels counter-intuitive. My expectation, since I (should) be using the cimported numpy, is that there should be very little python interaction.

My question is "is my intuition correct?". Have I set something up incorrectly with my files that is not allowing the cimported numpy to actually be used for the function call, and that is why I am still seeing so much yellow? Or am I fundamentally misunderstanding something.

Thanks for reading!

Defining the types as np.ndarray mainly improves one thing: it makes indexing them to get single values significantly faster. Almost everything else remains the same speed.

np.cumsum (and any other Numpy function) is called through the standard Python mechanism and runs at exactly the same speed (internally of course it's implemented in C and should be quite quick). Mathematical operator (such add +, -, *, etc. ) are also called through Python and remain the same speed.

In reality your wrapping probably makes it slower - it adds an unnecessary type-check (to make sure that the array is an np.ndarray ) and an extra layer of indirection.

There is nothing to be gained through typing here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM