我想优化这个简单的循环:
unsigned int i;
while(j-- != 0){ //j is an unsigned int with a start value of about N = 36.000.000
float sub = 0;
i=1;
unsigned int c = j+s[1];
while(c < N) {
sub += d[i][j]*x[c];//d[][] and x[] are arrays of float
i++;
c = j+s[i];// s[] is an array of unsigned int with 6 entries.
}
x[j] -= sub; // only one memory-write per j
}
对于 4000 MHz AMD Bulldozer,循环的执行时间约为一秒。我考虑过 SIMD 和 OpenMP(我通常使用它们来提高速度),但这个循环是递归的。
有什么建议吗?
最佳答案
认为您可能想要转置矩阵 d——意味着以可以交换索引的方式存储它——使 i 成为外部索引:
sub += d[j][i]*x[c];
而不是
sub += d[i][j]*x[c];
这应该会带来更好的缓存性能。
关于c++ - 我想优化这个短循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18262547/