Re: code optimization



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

.



Relevant Pages

  • Re: Efficiency of code generated by Ada compilers
    ... an Ada compiler would introduce in your, ... Usually the type of pixel (grayscale, ... that matter in C++ there are template specializations, ...
    (comp.lang.ada)
  • Re: Operation recomputation
    ... I in a loop have to compare ... it everytime does the shifting from the original foo value or if it ... The answer depends on whether your compiler can recognize the common ... example, I use that 10 times for each pixel I process, and ...
    (comp.lang.c)
  • Re: Operation recomputation
    ... it everytime does the shifting from the original foo value or if it ... The answer depends on whether your compiler can recognize the common ... in my case it's not a micro optimization since for my ... example, I use that 10 times for each pixel I process, and ...
    (comp.lang.c)
  • Re: Fine tuning register use with C# ?
    ... it will be probably more efficient to pack the RGB values of a pixel ... Software Design Engineer, CLR JIT Compiler ... > smart the JITer is (optimizing the switch statements, ... >> But you HAVE to attach the process with debugging to see this - normal ...
    (microsoft.public.dotnet.framework.performance)
  • RE: FOR070.DAT files appearing
    ... by international politics it is a safe bet that ISO will choose whatever ... Hmm, and Bill is an avid Fortran programmer :-) I'd use the term parochial in the wider sense of country. ... (One exception I will grant is the VAX Fortran compiler which is F77 ... If you have received the email in error, please notify TransGrid ...
    (comp.os.vms)