python - Cython 中的 ctypedef 与 numpy : what is right convention?

标签 python c numpy cython

在Cython中使用numpy的时候,这样写有什么意义:

cimport numpy as np
import numpy as np
ctypedef np.int_t DTYPE_t

然后到处使用 DTYPE_t 而不是只使用 np.int_tctypedef 实际上在此处的结果代码中做了什么不同的事情吗?

最佳答案

您可以阅读 docs for cython 中的注释, 阅读注释,他们解释了使用这种符号和导入的原因。

from __future__ import division
import numpy as np
# "cimport" is used to import special compile-time information
# about the numpy module (this is stored in a file numpy.pxd which is
# currently part of the Cython distribution).
cimport numpy as np
# We now need to fix a datatype for our arrays. I've used the variable
# DTYPE for this, which is assigned to the usual NumPy runtime
# type info object.
DTYPE = np.int
# "ctypedef" assigns a corresponding compile-time type to DTYPE_t. For
# every type in the numpy module there's a corresponding compile-time
# type with a _t-suffix.
ctypedef np.int_t DTYPE_t
# "def" can type its arguments but not have a return type. The type of the
# arguments for a "def" function is checked at run-time when entering the
# function.
#
# The arrays f, g and h is typed as "np.ndarray" instances. The only effect
# this has is to a) insert checks that the function arguments really are
# NumPy arrays, and b) make some attribute access like f.shape[0] much
# more efficient. (In this example this doesn't matter though.)

关于python - Cython 中的 ctypedef 与 numpy : what is right convention?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22083660/

相关文章:

c - 新线程的pid

python - 如何使用循环而不是内置函数在 Python 中生成 Toeplitz 矩阵

python - NumPy 用于设置行和列的索引

python - 使用滑动窗口和转置进行分组

python - 拆分测试集后如何使用 pandas 数据框?

python - 为什么 "os.sep"比 "os.path.join()"快?

python - 如何阻止 vs code 生成 .ipynb 文件?

python - 我如何使用 pandas 来透视这个基本表?

c - 一些指针澄清

c - typedef 定长数组