Re: code optimization
- From: Ulrich Eckhardt <doomster@xxxxxxxx>
- Date: Sat, 24 Sep 2005 09:42:39 +0200
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
.
- Follow-Ups:
- Re: code optimization
- From: Old Wolf
- Re: code optimization
- From: Syed
- Re: code optimization
- Prev by Date: Re: can't read file?
- Next by Date: Re: typedef
- Previous by thread: Re: Weird Issue
- Next by thread: Re: code optimization
- Index(es):
Relevant Pages
|