Cython :(为什么/何时)使用Py_ssize_t进行索引是否更可取? [英] Cython: (Why / When) Is it preferable to use Py_ssize_t for indexing?
问题描述
这是这个问题。
(为什么/何时)是否最好使用 Py_ssize_t
用于索引?在我刚发现的 docs 中
(Why / When) Is it preferable to use Py_ssize_t
for indexing? In the docs I just found
# Purists could use "Py_ssize_t" which is the proper Python type for
# array indices.
- >这是否意味着总是在索引NumPy / Cython时 - 数组(s)/ - 应该使用 Py_ssize_t
的视图?
-> Does that mean always when indexing NumPy/Cython - array(s)/-views one should use Py_ssize_t
?
- > Py_ssize_t
e。 G。一个 unsigned int
所以我不能用 @ cython.boundscheck(False)
-> Is Py_ssize_t
e. g. an unsigned int
so that I can't used @cython.boundscheck(False)
推荐答案
Py_ssize_t
已签名。请参阅 PEP 353 ,其中显示引入了新类型Py_ssize_t,其中包含与编译器的size_t类型大小相同,但是已签名。它将是ssize_t的typedef,如果可用的话。
Py_ssize_t
is signed. See PEP 353, where it says "A new type Py_ssize_t is introduced, which has the same size as the compiler's size_t type, but is signed. It will be a typedef for ssize_t where available."
你应该使用 Py_ssize_t
用于索引。我没有在Cython文档中找到明确的声明,但是Cython开发人员Stefan Behnel在电子邮件中说了很多(https://groups.google.com/forum/#!topic/cython-users/brENF_M9zxM ):
You should use Py_ssize_t
for indexing. I didn't find a definitive statement of this in the Cython docs, but Stefan Behnel, a Cython developer, said as much in an email (https://groups.google.com/forum/#!topic/cython-users/brENF_M9zxM):
作为一般性评论,您使用整数作为索引。你应该
使用Py_ssize_t类型(或者size_t,如果你更喜欢
无符号类型)来适当地适应64位架构。
As a general remark, you are using ints as indices. You should use the Py_ssize_t type instead (or size_t, if you prefer an unsigned type) to properly accommodate for 64 bit architectures.
这篇关于Cython :(为什么/何时)使用Py_ssize_t进行索引是否更可取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!