2013-10-12 76 views
-2

根據Visual Studio的性能分析器,以下函數正在消耗對我來說看起來異常大量的處理器能力,因爲它只是從幾個向量中添加1到3個數字,並將結果存儲在其中一個那些載體。如何優化這個處理大型C++向量的函數?

//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); 
} 

當然,所討論的載體具有約80000項大小,但通過比較,一個函數,它乘以複數的類似載體(複數乘法運算需要4次實數乘法和加法2各)消耗約三分之一處理器的能力。

也許它有一些事實,它必須在矢量內跳轉而不是直接訪問它們?但我真的不知道。任何想法如何可以優化?

編輯:我應該提到我也嘗試寫函數來線性訪問每個向量,但是這需要更多的總迭代,實際上性能更差。

+2

那麼這究竟是一個真正的瓶頸嗎?還是你只是看着它,並得出結論:「它消耗太多的CPU」? – 2013-10-12 23:26:46

+0

是的,它爲一個實時音頻應用程序,並且性能問題是可聽的 –

+1

你知道的東西可以作爲參考傳遞 –

回答