如何优化这个处理大型 c++ 向量的函数
How can I optimize this function which handles large c++ vectors?
根据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 的处理器功率。
也许它与它必须在向量内跳来跳去而不是只是线性访问它们有关?不过我真的不知道。关于如何优化它的任何想法?
编辑:我应该提到我也尝试编写函数来线性访问每个向量,但这需要更多的总迭代,实际上这种方式的性能更差。
根据需要启用编译器优化。 MSVC 指南在这里:http://msdn.microsoft.com/en-us/library/k1ack8f1.aspx
相关文章:
- 为什么我的代码无法处理大型数组输入(>10000)?
- opengl驱动程序在有限的VRAM中处理大型纹理阵列的效果如何
- 在C 中处理大型GZFILE
- 如何在C++中处理大型项目
- GDB 如何处理大型 (>1 Gb) 调试文件?
- 为大型本地数据处理扩展堆栈的大小是个好主意吗
- 如何优化这个处理大型 c++ 向量的函数
- 如何在大型项目中预处理具有多个 #include 文件的单个源文件
- 具有大型数组的 C++ 堆栈溢出未经处理的异常
- 处理大型数组
- 处理大型 C 数组时的内存问题
- 处理大型数组时超过时间限制
- 在OpenCV中处理大型图像
- 如何处理大型指针结构
- 使用C++AMP处理大型数组(65536+个元素)
- 如何使用Visual Studio处理大型非VS代码库
- 处理大型字符串/数组
- 在大型c++项目中处理配置的最佳方式
- 处理大型TIFF图像C++/Magick/libtiff
- std::映射<>或std::vector<>在处理大型标志集时