代码优化 [英] code optimization

查看:60
本文介绍了代码优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要线索来优化将

2维数组元素复制到另一个元素的函数。有没有办法优化这个代码

的代码,以便它运行得更快?


#define RIDX(i,j,n)((i) *(n)+(j))


void myrotate(int dim,pixel * src,pixel * dst)

{


for(i = 0; i< dim; i ++)

for(j = 0; j< dim; j ++)

dst [ RIDX(dim-1-j,i,dim)] = src [RIDX(i,j,dim)];

}

解决方案

在文章< 11 ********************* @ z14g2000cwz.googlegroups中。 com>,

syed< su ******* @ gmail.com>写道:

我想要线索来优化将
二维数组的元素复制到另一个的函数。有没有办法优化这段代码,以便它运行得更快?


可能不多。你正在改变元素的顺序,所以

你不能使用memcpy()。

#define RIDX(i,j,n)((i) *(n)+(j))




根据您对阵列的处理方式,您可能可以

避免复制它完全相反,而是使用不同版本的

RIDX来访问新订单中的元素。


- Richard


预先有一件事:C本身并没有定义任何类似性能的东西。我会

有点假设一个典型的桌面CPU。


syed写道:

我想要线索来优化一个函数将
二维数组的元素复制到另一个。有没有办法优化这段代码以便它运行得更快?

#define RIDX(i,j,n)((i)*(n)+(j) )


为什么?为什么不使用内联函数?

void myrotate(int dim,pixel * src,pixel * dst)


好​​的,这里有几点:

- 如果''dim''没有改变,你可以让它成为一个不变的给编译器

更好地优化代码的可能性。

- 你不打算修改''src'',所以把它指向const像素。

注意这不一定会影响性能。

- C99中有''restrict''属性,其中(AFAIK)可以用来告诉编译器两个范围不重叠的
。这可能使得事情更快,因为编译器可以更早地预取读取值。

{

for(i = 0; i< dim; i ++)


嗯,'我'在哪里?使它成为一个全球化并不会加快速度。另外:

断言(src&& dst&&(dim> 0));


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




嗯,这里可以做的不多。我会尝试的是计算一个指针

到内部循环中的行,以便不为每个像素重做这个计算

。也许是duff的设备会有所帮助,但我宁愿期待

当前的编译器自己展开循环。


最后,最简单的方法是什么时候不做 - 相反

你可以在访问矩阵时以不同方式计算索引。


Uli


您好


感谢您的回复,非常有帮助。我想使用单个循环(而不是两个)遍历

二维数组。是否可以支付



此外,代码片段中是否还有循环展开的空间?


再次感谢。

Syed Ijaz


-----

#define RIDX(i,j,n)( (i)*(n)+(j))


void myrotate(int dim,pixel * src,pixel * dst)

{


for(i = 0; i< dim; i ++)

for(j = 0; j< dim; j ++)

dst [RIDX(dim-1-j,i,dim)] = src [RIDX(i,j,dim)];

}


I want clues to optimize a function that copies elements of a
2-dimensional array to another. Is there a way to optimize this piece
of code so that it runs faster?

#define RIDX(i,j,n) ((i)*(n)+(j))

void myrotate(int dim, pixel *src, pixel *dst)
{

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

解决方案

In article <11*********************@z14g2000cwz.googlegroups. com>,
syed <su*******@gmail.com> wrote:

I want clues to optimize a function that copies elements of a
2-dimensional array to another. Is there a way to optimize this piece
of code so that it runs faster?
Probably not much. You are changing the order of the elements, so
you can''t use memcpy().
#define RIDX(i,j,n) ((i)*(n)+(j))



Depending on what you are doing with the array, you might be able to
avoid copying it altogether, and instead use a different version of
RIDX to access the elements in the new order.

-- Richard


One thing up front: C itself doesn''t define anything like performance. I''ll
somewhat assume a typical desktop CPU.

syed wrote:

I want clues to optimize a function that copies elements of a
2-dimensional array to another. Is there a way to optimize this piece
of code so that it runs faster?

#define RIDX(i,j,n) ((i)*(n)+(j))
Why? Why not use an inline function?
void myrotate(int dim, pixel *src, pixel *dst)
Okay, a few points here:
- If ''dim'' doesn''t change, you could make it a constant giving the compiler
possibility to optimise code better.
- You''re not going to modify ''src'', so make that a pointer to const pixel.
Note that this won''t necessarily influence performance.
- There''s the ''restrict'' property in C99 which (AFAIK) can be used to tell
the compiler that the two ranges don''t overlap. This might make things
faster as the compiler can prefetch the read-values earlier.
{

for (i = 0; i < dim; i++)
Hmm, where is ''i''? Making it a global doesn''t speed up things. Also:
assert(src && dst && (dim>0));

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



Hmm, not much that can be done here. What I''d try is to compute a pointer
to the row in the inner loop in order not to redo this computation for
every pixel. Maybe a "duff''s device" would help, but I''d rather expect
current compilers to unroll loops themselves.

Lastly, the probably fastest way is when this is simply not done - instead
you could compute the index differently when accessing the matrix.

Uli


Hi

Thanks for your reply, it was very helpful. I would like to iterate through
the 2-dimensional array using a single loop (instead of two). Is it
possible?

Also, is there any room for loop-unrolling in the code snippet?

Thanks again.
Syed Ijaz

-----
#define RIDX(i,j,n) ((i)*(n)+(j))

void myrotate(int dim, pixel *src, pixel *dst)
{

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


这篇关于代码优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆