如果我这样做...
vector< array<double,2> > magT(magnitude.size(), array<double,2>() );
for ( int n = 0; n < magnitude.size()-amount; n++ ) {
for ( int chan = 0; chan < channels; chan++ ) {
for ( int m = 0; m < amount; m++ ) {
magT.at(n).at(chan) += magnitude.at(n+m).at(chan);
}
magT.at(n).at(chan) /= amount;
}
}
magnitude = magT;
...闪电般的速度。但是,如果我添加几个这样的 if 语句......
vector< array<double,2> > magT(magnitude.size(), array<double,2>() );
int upper = ceil(amount/2);
int lower = floor(-amount/2);
for ( int n = 0; n < magnitude.size(); n++ ) {
for ( int chan = 0; chan < channels; chan++ ) {
for ( int m = lower; m < upper; m++ ) {
m = n+m < 0 ? 0 : m;
m = n+m > magnitude.size()-1 ? 0 : m;
magT.at(n).at(chan) += magnitude.at(n+m).at(chan);
}
magT.at(n).at(chan) /= amount;
}
}
magnitude = magT;
...它慢了数百倍,也许是数千倍。逻辑语句真的这么慢吗?我应该让它们远离这样的嵌套循环吗?如果是这样,我如何合理地完成这样的事情?