为何计算数组移动L2范数的代码运行时间差异近5倍?
数组移动L2范数计算性能差异排查求助
我实现了一段计算两个数组移动L2范数的C++代码:
func_lstl2(const int &nx, const float x[],const int &ny, const float y[], int &shift, double &lstl2) { int maxshift = 200; int len_z = maxshift * 2; int len_work = len_z + ny; //initialize array work and array z double *z = new double[len_z]; float *work = new float[len_work]; for (int i = 0; i < len_z; i++) z[i] = 0; for (int i = 0; i < len_work; i++) work[i] = 0; for (int i = 0; i < ny; i++) work[i + maxshift] = y[i]; // do moving least square residue calculation float temp; for (int i = 0; i < len_z; i++) { for (int j = 0; j < nx; j++) { temp = x[j] - work[i + j]; z[i] += temp * temp; } } // find the best fit value lstl2 = 1E30; shift = 0; for (int i = 0; i < len_z; i++) { if (z[i] < lstl2) { lstl2 = z[i]; shift = i - maxshift; } } //end of program delete[] z; delete[] work; }
我测试了两组长度均为2000的数组,调用代码如下:
int shift; double lstl2; func_lstl2(2000,z1,2000,z2,shift,lstl2) ; func_lstl2(2000,x1,2000,x2,shift,lstl2) ;
奇怪的是,处理z数组耗时0.0032346秒,处理x数组却耗时0.0140903秒,运行时间差了近5倍。我找不到问题所在,恳请帮忙排查。相关数组文件已提供。
内容的提问来源于stack exchange,提问作者YS_Jin
相关产品推荐
相关产品推荐

