RES: [Dev-C++] Optimizing C code, what is best??
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Daniel K. O. <dan...@ya...> - 2003-05-30 20:01:23
|
From Per:
> while (src <= end) {
> iA = *src++ - *sp++;
> dwDist2 += iA*iA;
> }
From Zodiaq:
> > for( WORD wCol = wColFrom; wCol <= wColTo; ++wCol )
> > {
> > iA = src[ wCol ] - sP[ wCol ];
> > dwDist += iA * iA;
> > }
It was really funny those recent comments about "the best way to write C
code for optimization".
Yeah, some years ago most compilers were just "smart assemblers". But
nowadays, worrying about stuffs like 'array[i++]' vs. '*pointer++' trying to
make it "easier" to the compiler doesn't make much sense. If your compiler
can't figure obvious stuffs - like iterating throgh an array - and make the
best code for it, the best way is to find a better compiler.
And GCC is a mature compiler; comments like "it can optimize in a way it
won't work" without any test-case that proves it has a *serious* bug, is a
little malicious.
Just before writing this mail, I tried with a small sample:
---
int foo2(int vet1[], int vet2[], int n)
{
int* i1 = vet1, *i2 = vet2, *j = vet1+n;
int c;
while(i1!=j)
c += *i1++ + *i2++;
return c;
}
int foo1(int vet1[], int vet2[], int n)
{
int i, c;
for (i=0; i<n; i++)
c += vet1[i] + vet2[i];
return c;
}
---
And compiled it with:
-S -march=pentium3 -O2
The results:
foo1 -- 30 lines
foo2 -- 29 lines
With just 1 instruction less (for this small example), I don't see the point
in rewriting the code to make it faster. If I want to spend my time making
it faster, better do it directly in assembly, not through C.
So, my question: do you guys think that really worth being so "paranoic"
about such details? I mean, obviously the algorithm design is important, but
is really needed to even care about the different ways to write the same
command, to see "which one is faster"?
Daniel K. O.
|