【发布时间】:2014-09-25 23:11:34
【问题描述】:
如何高效地将 cython 中的 malloc 数组指针(或 numpy 数组指针)返回给 python3。
只要我不返回数组指针,cython 代码就可以完美运行
我想要:
def double complex* randn_zig(int n):
...
r = malloc(n*n*sizeof(double complex))
...
return r
等效的 c11 (gcc 11) 是:
double complex* randn_zig(int n){
r = malloc(n*n*sizeof(double complex))
return r
}
我试过了
<double complex*> randn_zig(int n):
和randn_zig(<double complex*> r, int n):
和其他排列到目前为止没有成功。如果我能找到一种方法来返回指向大型 10^6 到 10^10 双复数数组的指针,c 和 cython 代码版本的速度是 Numby/pylab randn 版本的 5 倍。
【问题讨论】:
-
当你试图调用这个函数时,你能提供更多的代码吗?您会遇到哪些错误?
-
没有与指针等效的python,所以它不是你想要返回的指针,它是对已分配内存块的一些引用(即,一些具有指向指针的python对象) .您选择哪种 python 对象取决于您打算如何处理指针/内存块。
标签: python c arrays numpy cython