根据Visual Studio的性能分析器,以下功能正在消耗我看来是异常大量的处理器能力,因为它所做的就是从几个向量中添加1到3个数字并将结果存储在一个那些载体。
//Relevant class members:
//vector<double> cache (~80,000);
//int inputSize;
//Notes:
//RealFFT::real is a typedef for POD double.
//RealFFT::RealSet is a wrapper class for a c-style array of RealFFT::real.
//This is because of the FFT library I'm using (FFTW).
//It's bracket operator is overloaded to return a const reference to the appropriate array element
vector<RealFFT::real> Convolver::store(vector<RealFFT::RealSet>& data)
{
int cr = inputSize; //'cache' read position
int cw = 0; //'cache' write position
int di = 0; //index within 'data' vector (ex. data[di])
int bi = 0; //index within 'data' element (ex. data[di][bi])
int blockSize = irBlockSize();
int dataSize = data.size();
int cacheSize = cache.size();
//Basically, this takes the existing values in 'cache', sums them with the
//values in 'data' at the appropriate positions, and stores them back in
//the cache at a new position.
while (cw < cacheSize)
{
int n = 0;
if (di < dataSize)
n = data[di][bi];
if (di > 0 && bi < inputSize)
n += data[di - 1][blockSize + bi];
if (++bi == blockSize)
{
di++;
bi = 0;
}
if (cr < cacheSize)
n += cache[cr++];
cache[cw++] = n;
}
//Take the first 'inputSize' number of values and return them to a new vector.
return Common::vecTake<RealFFT::real>(inputSize, cache, 0);
}
当然,所讨论的向量具有大约80,000个项目的大小,但相比之下,复数的相似向量乘以的函数(复数乘法需要4个实数乘法和每个2个加法)消耗大约1/3处理器功率。
也许它有一些事实,它必须在向量内跳转而不是线性地访问它们?我真的不知道。关于如何优化这一点的任何想法?
编辑:我应该提到我也尝试编写函数来线性访问每个向量,但这需要更多的总迭代次数,实际上性能更差。