简体   繁体   English

Cython:在纯 Python 模式下有效使用 Numpy

[英]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.我对使用 Cython 还很陌生,我对使用“纯 Python”模式很感兴趣。

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.我现在正在做的工作广泛使用 numpy,并且知道 numpy 有一个 C api,我很高兴看到它可以做什么。

As a small test, I put together two small test files, test.py and test.pxd .作为一个小测试,我将两个小测试文件放在一起, test.pytest.pxd Their content is as follows:它们的内容如下:

test.py:测试.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:测试.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().然后我用cython -a test.py编译了这些文件,希望在调用 np.cumsum() 时几乎看不到 python 交互。 However when I inspected the generated HTML file, I found the following:但是,当我检查生成的 HTML 文件时,我发现了以下内容:

在此处输入图片说明

From this, it appears that my call to np.cumsum heavily interacts with python, which is something that feels counter-intuitive.由此看来,我对 np.cumsum 的调用似乎与 python 有很大的交互,这有点违反直觉。 My expectation, since I (should) be using the cimported numpy, is that there should be very little python interaction.我的期望,因为我(应该)使用 cimported numpy,应该有很少的 python 交互。

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?我是否对我的文件进行了错误设置,不允许 cimported numpy 实际用于函数调用,这就是为什么我仍然看到这么多黄色? 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.将类型定义为np.ndarray主要改进了一件事:它使索引它们以更快地获得单个值。 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). np.cumsum (和任何其他 Numpy 函数)通过​​标准 Python 机制调用并以完全相同的速度运行(当然它在内部是用 C 实现的,应该很快)。 Mathematical operator (such add +, -, *, etc. ) are also called through Python and remain the same speed.数学运算符(如加+, -, *, etc. )也通过 Python 调用并保持相同的速度。

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.实际上,您的包装可能会使其变慢 - 它增加了不必要的类型检查(以确保数组是np.ndarray )和额外的间接层。

There is nothing to be gained through typing here.在这里打字没有任何好处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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