Cython:(为什么/何时)使用 Py_ssize_t 进行索引是否更可取? [英] Cython: (Why / When) Is it preferable to use Py_ssize_t for indexing?

查看:25
本文介绍了Cython:(为什么/何时)使用 Py_ssize_t 进行索引是否更可取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是这个问题.

(为什么/何时)使用 Py_ssize_t 进行索引是否更可取?在 docs 我刚刚找到

<块引用>

# Purists 可以使用Py_ssize_t",这是正确的 Python 类型# 数组索引.

-> 这是否意味着在索引 NumPy/Cython 时总是 - 数组/视图应该使用 Py_ssize_t?

-> 是 Py_ssize_t e.G.一个 unsigned int 这样我就不能使用 @cython.boundscheck(False)

解决方案

Py_ssize_t 已签名.请参阅 PEP 353,其中说引入了一种新类型 Py_ssize_t,它具有与编译器的 size_t 类型大小相同,但有符号.如果可用,它将是 ssize_t 的 typedef."

您应该使用 Py_ssize_t 进行索引.我没有在 Cython 文档中找到明确的声明,但是 Cython 开发人员 Stefan Behnel 在一封电子邮件中说了同样多的话(https://groups.google.com/forum/#!topic/cython-users/brENF_M9zxM):

<块引用>

一般来说,您使用整数作为索引.你应该改用 Py_ssize_t 类型(或 size_t,如果你喜欢无符号类型)以正确适应 64 位架构.

This is a follow-up to this question.

(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.

-> Does that mean always when indexing NumPy/Cython - array(s)/-views one should use Py_ssize_t?

-> Is Py_ssize_t e. g. an unsigned int so that I can't used @cython.boundscheck(False)

解决方案

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."

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):

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆