【发布时间】:2010-07-25 17:42:02
【问题描述】:
我在 python 中编织我的 c 代码以加快循环:
from scipy import weave
from numpy import *
#1) create the array
a=zeros((200,300,400),int)
for i in range(200):
for j in range(300):
for k in range(400):
a[i,j,k]=i*300*400+j*400+k
#2) test on c code to access the array
code="""
for(int i=0;i<200;++i){
for(int j=0;j<300;++j){
for(int k=0;k<400;++k){
printf("%ld,",a[i*300*400+j*400+k]);
}
printf("\\n");
}
printf("\\n\\n");
}
"""
test =weave.inline(code, ['a'])
它工作得很好,但是当数组很大时它仍然很昂贵。 有人建议我使用 a.strides 而不是讨厌的“a[i*300*400+j*400+k]” 我无法理解有关 .strides 的文档。
任何想法
提前致谢
【问题讨论】: