-2

根据 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 的处理器功率。

也许这与它必须在向量内跳转而不是仅仅线性访问它们有关?不过我真的不知道。关于如何优化它的任何想法?

编辑:我应该提到我还尝试编写函数来线性访问每个向量,但这需要更多的总迭代次数,实际上性能更差。

4

1 回答 1

1

根据需要打开编译器优化。MSVC 指南在这里:http: //msdn.microsoft.com/en-us/library/k1ack8f1.aspx

于 2013-10-13T02:06:27.957 回答