C画面旋转优化

标签 c optimization rotation pixel

这是给所有 C 专家的..

第一个函数采用表示图像像素的二维矩阵 src[dim][dim],并将其旋转 90 度成为目标矩阵 dst[dim][dim]。第二个函数采用相同的 src[dim][dim] 并通过将每个像素值替换为其周围所有像素的平均值(在以该像素为中心的最大 3 × 3 窗口中)来平滑图像。

我需要根据时间和周期优化程序,我还能如何优化以下内容?:

void rotate(int dim, pixel *src, pixel *dst,) 
{
    int i, j, nj;
    nj = 0;

    /* below are the main computations for the implementation of rotate. */
    for (j = 0; j < dim; j++) {
        nj = dim-1-j;               /* Code Motion moved operation outside inner for loop */
        for (i = 0; i < dim; i++) {
            dst[RIDX(nj, i, dim)] = src[RIDX(i, j, dim)];
        }
    }
}



/* A struct used to compute averaged pixel value */
typedef struct {
    int red;
    int green;
    int blue;
    int num;
} pixel_sum;

/* Compute min and max of two integers, respectively */
static int minimum(int a, int b) 
{ return (a < b ? a : b); }
static int maximum(int a, int b) 
{ return (a > b ? a : b); }

/* 
 * initialize_pixel_sum - Initializes all fields of sum to 0 
 */
static void initialize_pixel_sum(pixel_sum *sum) 
{
    sum->red = sum->green = sum->blue = 0;
    sum->num = 0;
    return;
}

/* 
 * accumulate_sum - Accumulates field values of p in corresponding 
 * fields of sum 
 */
static void accumulate_sum(pixel_sum *sum, pixel p) 
{
    sum->red += (int) p.red;
    sum->green += (int) p.green;
    sum->blue += (int) p.blue;
    sum->num++;
    return;
}

/* 
 * assign_sum_to_pixel - Computes averaged pixel value in current_pixel 
 */
static void assign_sum_to_pixel(pixel *current_pixel, pixel_sum sum) 
{
    current_pixel->red = (unsigned short) (sum.red/sum.num);
    current_pixel->green = (unsigned short) (sum.green/sum.num);
    current_pixel->blue = (unsigned short) (sum.blue/sum.num);
    return;
}

/* 
 * avg - Returns averaged pixel value at (i,j) 
 */
static pixel avg(int dim, int i, int j, pixel *src) 
{
    int ii, jj;
    pixel_sum sum;
    pixel current_pixel;

    initialize_pixel_sum(&sum);
    for(ii = maximum(i-1, 0); ii <= minimum(i+1, dim-1); ii++) 
        for(jj = maximum(j-1, 0); jj <= minimum(j+1, dim-1); jj++) 
            accumulate_sum(&sum, src[RIDX(ii, jj, dim)]);

    assign_sum_to_pixel(&current_pixel, sum);
    return current_pixel;
}

void smooth(int dim, pixel *src, pixel *dst) 
{
    int i, j;


    /* below are the main computations for the implementation of the smooth function. */

    for (j = 0; j < dim; j++)
        for (i = 0; i < dim; i++)
            dst[RIDX(i, j, dim)] = avg(dim, i, j, src);
}

我将 dim-1-j 移到了 rotate 的内部 for 循环之外,这减少了程序中使用的时间和周期,但是是否还有其他任何东西可以用于任一 main 函数?

谢谢!

最佳答案

您可以进行多种优化;一些编译器可能会为你做,但最好自己写出来。例如:将常量表达式移出循环(你做过一次;还有更多地方你可以这样做 - 不要忘记每次迭代都会检查条件,所以也以这种方式优化循环条件)并且,正如 Chris 指出的那样,使用递增的指针而不是完整的数组索引。我还看到了一些可以在线重写的函数调用。

我还想指出一篇关于 stackoverflow 的文章,内容涉及矩阵乘法和优化它以使用处理器缓存。本质上,它首先将数组重新排列到适合缓存的内存块中,然后对这些 block 执行操作,然后移动到下一个 block ,依此类推。您可以将这些想法重新用于您的轮换。 参见 Optimizing assembly generated by Microsoft Visual Studio Compiler

关于C画面旋转优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27327144/

相关文章:

c++ - 如果我在同一语句中使用预增量和后增量会怎样?

c - 插入数组期间整数值发生变化

android - 在旋转时我得到 "Unable to destroy activity ... Can not perform this action after onSaveInstanceState"

android - 每当设备旋转时,使用 libgdx 的应用程序都会重新启动

Javascript:使用性能参数对函数进行原型(prototype)设计

c# - Matrix.RotateAt 方法对于某些角度是否存在错误? .Net Winforms

c - 如果0在c程序中编译

c - 了解标识符的链接

c - rdtsc() 给出奇怪的结果

c++ - Visual C++ 逗号运算符和 sse 内在函数