定义类型
fid将无济于事,因为调用python函数仍然很昂贵。尝试使用“
-a”标志编译示例以了解我的意思。但是,可以将低级C函数用于文件处理,以避免循环中的python开销。出于示例的考虑,我假设数据从文件的开头开始,并且其类型为
double
from libc.stdio cimport *cdef extern from "stdio.h": FILE *fdopen(int, const char *)import numpy as npcimport numpy as npDTYPE = np.double # or whatever your type isctypedef np.double_t DTYPE_t # or whatever your type isdef FromFileSkip(fid, int count=1, int skip=0): cdef int k cdef FILE* cfile cdef np.ndarray[DTYPE_t, ndim=1] data cdef DTYPE_t* data_ptr cfile = fdopen(fid.fileno(), 'rb') # attach the stream data = np.zeros(count).astype(DTYPE) data_ptr = <DTYPE_t*>data.data # maybe skip some header bytes here # ... for k in range(count): if fread(<void*>(data_ptr + k), sizeof(DTYPE_t), 1, cfile) < 0: break if fseek(cfile, skip, SEEK_CUR): break return data
请注意,的输出
cython -a example.pyx显示循环内没有python开销。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)