|
From: Abhijit S. <mu...@gm...> - 2003-08-04 18:44:04
|
Hello, Oroszi.
> for(i=0;i<getint();i++){
> /* ... */
> }
>
> Would it worth it to take that getint() into an outer variable, and only
> use that in the conditional, like this:
>
> int c = getint();
> for(i=0;i<c;i++){
> /* ... */
> }
>
> Will this give me any speedup?
I don't know about the working of gcc, so this I don't know if this will be
optimized. However, there's no loss in doing this, and I guess you should
(so that even if gcc, or whatever compiler you want to move it onto doesn't
optimize it, you get the fastest code). Without optimization, the second
will definitely be better than the first, since in case 1, on every
iteration, there's the overhead of the function call, and in case 2, that is
eliminated.
> I am interested in particular, is for
> going to call getint() EVERY time in the first case (on top), or will it
> do it only once? I think the second case is better, am I right?
I think I answered that already.
> Allright, that was my first question, here is the second:
> What is the case, if the conditional is a KNOWN value. Something, like a
> macro. Here is the example:
>
> #define GETINT (10*5)
>
> for(i=0;i<GETINT;i++){
> /* ... */
> }
There is no need for optimization at all here, because the compiler will see
this as:
for(i = 0; i < (10*5); i++) { [whatever] }
So that's a constant already. The unrolling may be done by the compiler
itself (there was a flag -funroll-loops, I'm not sure, you should check that
out).
The postfix/prefix conversion indicated by Charles will help only for
user-defined classes, not for primitives. Also, I think gcc optimizes this
automatically.
This might give you more info:
http://computational-battery.org/Tools/gcc-opt-guide.html
There are a couple of links at the bottom of the page that you should read,
too.
--
Abhijit Shylanath
Homepage: http://mudeth.tripod.com/
|