cython 编译 - 导入与 cimport

cython compilation - import vs cimport

Cython 新手(也许这是一个基本问题)。考虑两个例子都来自 this blog here:

# code 1
import numpy as np

def num_update(u):
    u[1:-1,1:-1] = ((u[2:,1:-1]+u[:-2,1:-1])*dy2 + 
                    (u[1:-1,2:] + u[1:-1,:-2])*dx2) / (2*(dx2+dy2))

# code 2
cimport numpy as np

def cy_update(np.ndarray[double, ndim=2] u, double dx2, double dy2):
    cdef unsigned int i, j
    for i in xrange(1,u.shape[0]-1):
        for j in xrange(1, u.shape[1]-1):
            u[i,j] = ((u[i+1, j] + u[i-1, j]) * dy2 +
                      (u[i, j+1] + u[i, j-1]) * dx2) / (2*(dx2+dy2))

假设我使用以下 setup.py 脚本编译第一个文件:

# setup file for code 1
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext = Extension("laplace", ["laplace.pyx"],)
setup(ext_modules=[ext], cmdclass = {'build_ext': build_ext})

第二个文件包含以下 setup.py 脚本:

# setup file for code 2
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

import numpy

ext = Extension("laplace", ["laplace.pyx"],
    include_dirs = [numpy.get_include()])

setup(ext_modules=[ext], cmdclass = {'build_ext': build_ext})

在第一种情况下,我使用常规 numpy 并且没有在设置文件中导入 numpy,而在第二种情况下,我使用 [=19= 导入 numpy ],使用 cdef 声明变量,但随后也在安装文件中包含 numpy

Cython 无论如何都会编译第一个代码(并且第一个代码似乎可以工作)。

在用 Cython 编译之前(通过设置文件)使用 cimportcdef 与在用 Cython 编译之前不使用 cimportcdef 相比有什么好处(通过安装文件)?

Cython 可以编译正常的 python 代码,因此您的第一个可以编译。

一般来说,为 cython 标记的类型越多,获得更好性能的机会就越大。因此,是否要以灵活性换取速度由您决定。

运行 cython -a your_test.pyx 查看 cython 如何编译代码的注释版本。黄色表示您的代码转换为大量 C 代码(大致意味着性能损失),而白色表示它仅转换为几行 C。

如果你没有花时间在这里问,而是通过阅读官网的guide,你可能已经有了更好的理解。

import numpy 在 Cython 中与 Python 相同,但是 cimport numpy 告诉 Cython 加载声明文件:

https://github.com/cython/cython/blob/master/Cython/Includes/numpy/init.pxd

其中声明了所有 C-API 函数、常量和类型,还包括头文件,例如 numpy/arrayobject.h.

如果您使用 np.ndarray[...] 声明变量,Cython 将知道如何将数组元素访问转换为 c 代码,这比 Python 的 [] 运算符快得多。

你需要告诉c编译器setup.py中的numpy头文件在哪里,所以你调用numpy.get_include()来获取路径。