简体   繁体   English

C / C ++脚本优化

[英]C/C++ script optimization

I have 3 matrices, gX , gY and gNorm , and one for loop like this: 我有3个矩阵gXgYgNorm ,一个这样的循环:

for(y=1; y<n-1; y++){
      int base = y*this->_nxsIn;
      for(x=1; x<n-1; x++){ 
          i1 = base + x;
          i2 = i1 + 1;
          i3 = i1 + n;
          den = ( (gX[i1]+gY[i1])*gNorm[i1] - gX[i2]*gNorm[i2] - gY[i3]*gNorm[i3];
}

If I extract the products gX * gNorm and gY * gNorm in this way: 如果我以这种方式提取产品gX * gNormgY * gNorm

for(y=0; y<n; y++){
      int base = y*n;
      for(x=0; x<n; x++){
           i = base + x;
          GxGnorm[i] = gX[i]*gNorm[i];
       }
   }

//Gy * GNorm

for(y=0; y<n; y++){
     int base = y*n;
     for(x=0; x<n; x++){
          i = base + x;
         GyGnorm[i] = gY[i]*gNorm[i];
      }
}


for(y=1; y<this->_nysIn-1; y++){
      int base = y*this->_nxsIn;
      for(x=1; x<this->_nxsIn-1; x++){  
      i1 = base + x;
      i2 = i1 + 1;
      i3 = i1 + n;

      i1Sum = GxGnorm[i1] + GyGnorm[i1];         //(gX[i1]+gY[i1])*gNorm[i1];
      float sum1 = GxGnorm[i2] + GyGnorm[i3];    //gX[i2]*gNorm[i2];

     }
}

Can I get improvements in computation time? 我可以改善计算时间吗?

If no, why? 如果没有,为什么?

Well, both scripts have the same asymptotic time, O(n^2), which is bad if you have a substantial data set. 好吧,这两个脚本的渐近时间都相同,即O(n ^ 2),如果您有大量数据集,那么这是不好的。

As you have repetitions of the nested loops in your second script, you are actually increasing the constant factor. 当您在第二个脚本中重复嵌套循环时,实际上是在增加常量因子。 Add to that the costs of creating the extra arrays. 再加上创建额外阵列的成本。

Other than these both scripts are quite similar. 除了这两个脚本,其他脚本都非常相似。 So, the second script might actually be slower. 因此,第二个脚本实际上可能会更慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM