【问题标题】:Does typing increase speed of execution when wrapping C functions with Cython?用 Cython 包装 C 函数时,打字会提高执行速度吗?
【发布时间】:2023-03-13 02:45:01
【问题描述】:

我正在使用 Cython 来包装一些 C 代码。这是一个典型例程的声明(实际包含大约 13 到 17 个参数)。

void my_func(double *__restrict__ lhs, const int xlhs, const int oxlhs);

现在,在 .pyx 文件中,这是我定义包装器的方式

def preCond1D(np.ndarray[double, ndim=2] lhs not None, xlhs not None, oxlhs not None): 
   preCond1D_func( <double*> np.PyArray_DATA(lhs), xlhs, oxlhs)

如果我明确声明(即键入)int 变量,是否会有任何好处(性能或其他方面)

def preCond1D(np.ndarray[double, ndim=2] lhs, xlhs: cython.int32, oxlhs: cython.int32): 
   preCond1D_func( <double*> np.PyArray_DATA(lhs), xlhs, oxlhs)

【问题讨论】:

  • AFAIK,类型提示在 Python 中无效。
  • 我明白了,但是这个 .pyx 文件是用 C 语言翻译然后编译的,所以它可以添加额外的类型检查层
  • 可以,但我认为不会。

标签: python c cython


【解决方案1】:

Cython 确实使用类型提示,因此它们通常很有用。您可以通过annotation_typing 指令来控制它。

在这种情况下,尽管它们完全没有区别。您将获得一组从 Python 值到 C 类型的数据转换。使用类型提示,它们将出现在preCond1D 的开头。如果没有类型提示,它们将在调用 preCond1D_func 时发生。

唯一的好处是类型提示提供的文档。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-09
  • 1970-01-01
  • 2019-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多