【问题标题】:Looking for ways to optimize this code寻找优化此代码的方法
【发布时间】:2016-07-30 01:08:53
【问题描述】:

以下代码是边缘检测程序的一部分:

void detect_optimized(int width, int height, int threshold)
{
    int x, y;
    int tmp;`
    int w = width--;
    int h = height--;

for (y = 1; y < w; y++)
    for (x = 1; x < h; x++)
    {
        tmp = mask_product(mask,a,x,y,0);
        if (tmp>255)
            tmp = 255;
        if (tmp<threshold)
            tmp = 0;
        c[x][y][0] = 255-tmp;

        tmp = mask_product(mask,a,x,y,1);
        if (tmp>255)
            tmp = 255;
        if (tmp<threshold)
            tmp = 0;
        c[x][y][1] = 255-tmp;

        tmp = mask_product(mask,a,x,y,2);
        if (tmp>255)
            tmp = 255;
        if (tmp<threshold)
            tmp = 0;
        c[x][y][2] = 255-tmp;
    }
}

我一直在尝试使用以下代码实现阻塞,但我不确定在这种情况下如何使用它。

【问题讨论】:

  • 非常小,但您可以在每对中使用else if 作为第二个if
  • 我假设 threshold 应该始终小于 255。
  • 为什么int w = width--; 然后没有进一步使用width 所以没有减少点?使用widthheight作为循环限制变量,去掉wh,如for (y = 1; y &lt; width; y++),效率低。您还可以通过将特殊情况直接写入数组(而不是做减法)来获得一些效率,然后是else
  • byte c[MAX_ROW][MAX_COL][NUM_COLORS];
  • 奇怪地类似于this recent question

标签: c loops optimization


【解决方案1】:

您可以交换循环以获得更好的缓存利用率。这应该会显着加快您的代码速度(尤其是对于大数据)。

for (x = 1; x < h; x++)
    for (y = 1; y < w; y++)

通过将循环迭代分布在多个线程上以利用多核架构,可以获得另一个实质性的好处。使用OpenMP 可以通过如下的单个编译器指令来实现。

#pragma omp parallel for private(y, tmp)
for (x = 1; x < h; x++)
    for (y = 1; y < w; y++)

其他优化通常由编译器完成。确保使用适当的编译器标志,例如 -O2,并且不要自己为低级适配而烦恼。

【讨论】:

    【解决方案2】:

    提供以下候选人

    1. * 的价格避开if()s。各种流水线平台将受益。
    2. 交换x,y订单
    3. 递减,因此循环结束测试针对 0。
    4. 避免重新计算c[x][y]

    假设需要遍历所有颜色。

    当然,YMMV。

    for (x = h-1; x > 0; x--) {
      byte *p = &c[x][w-1][NUM_COLORS-1];
    
      for (y = w-1; y > 0; y--) {
        for (int z = NUM_COLORS-1; z >= 0; z--) {
          int tmp = mask_product(mask,a,x,y,z);
          *p = (255 - tmp*(tmp>=threshold))*(tmp <=255);
          p--;
         }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-23
      • 1970-01-01
      • 2016-09-26
      • 2022-07-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多