• Advertisement
Sign in to follow this  

Interesting loop..

This topic is 4929 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Would you consider this OK programming? int i=500; while(i--){ //do stuff in loop. } Would that be equivilent to a for (int i=0;i<500;i++){//do stuff} and would it be faster?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
The for loop goes from 0 to 499, and the while loop goes from 500 to 1, so they are not equivalent, if you use the index i for something inside the loop. If you don't use i inside the loop, then it should be equivalent. I don't know which is faster, but it probably depends on the compiler also.

Share this post


Link to post
Share on other sites
Yah, I wasn't planning on using the index for anything, just repeating a certain thing 500 times quickly.

I guess this kind of boils down to...

which is faster..

int i=0;
if (i!=0){}

or....

int i=0;
if (i){}

Share this post


Link to post
Share on other sites
These two loops are similiar but still they are not the same - and I am no talking about different coding style.

You see - in this situation you can't say if some method is OK or not. Everything depends on what you want to achieve. If, for example, you'd like to have a loop counter i going down from 500 to 0 you'd probably use the first loop. In case of a counter going from 0 to 500 you'd go for the second solution. Anyway, it doesn't mean that you can't do a "for loop" with a counter which decreases in each iteration.

In my opinion both methods have the same execution speed but still - it's not a part of code where you should for performance benefits.

I would say that the main difference (despite the increasing/decreasing loop counter mentioned before) is that i in the second method is declared only in a scope of the loop while in the first example i is accesible even when the loop finishes.

See ya'

Share this post


Link to post
Share on other sites
no, the first loop will be from i=500 until i=0 (501 loops)
the second loop will be from i=0 until i=499 (500 loops)
changing the second loop to:
for (int i=0;i<=500;i++){//do stuff}

will be like as the first loop.
but the second loop will be the same if it was:
for (int i=500;i--;){//do stuff}

if you need 'i' for the loop only then it will be better to use the for (), but you can still use while ().
i dont know what will be faster, i think their speed is equal (exept 'int i=500', maybe for() is faster if you use the 'i' for the loop only.).
but their speed shouln't really bother, computers are really fast machines.

Share this post


Link to post
Share on other sites
Quote:
Original post by clapton
I would say that the main difference (despite the increasing/decreasing loop counter mentioned before) is that i in the second method is declared only in a scope of the loop while in the first example i is accesible even when the loop finishes.

See ya'

not in VC 6, but i think its true in .NET compilers.

Share this post


Link to post
Share on other sites
Quote:
Original post by Adams555
which is faster..
Neither. Both. Who cares?

Don't worry about speed until you know you need it. Until then, emphasize correctness.

Share this post


Link to post
Share on other sites
If you want an equivalent set of array indices, use --i in the while. It will still be in reverse order, of course. Which may interfere with correctness, of course.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
it's easier to make correct code fast than it is to make fast code correct.

Share this post


Link to post
Share on other sites
The two loops the OP posted are identical, except the while is the reverse of the for - both loops can be used to index into an array declared:

some_type array [500];

Don't believe me? Then try this:

#include <stdio.h>
int main (void)
{
int
i = 500,
total1 = 0,
total2 = 0;

// sum all possible values of i in both versions
// i + 10 is used so the the case of i==0 is not ignored
while (i--)
{
total1 += i + 10;
}
for (i = 0 ; i < 500 ; i++)
{
total2 += i + 10;
}
printf ("total1 = %d, total2 = %d\n", total1, total2);
return 0;
}


To get technical about it, the compiler produced the following results (MSVC6 with default optimisations):

loop instructions bytes
for 2/4 4/12
while 2/5 7/11
hand coded asm 2/2 7/6

The figures are: setup / loop, where setup is the code executed before the loop and loop is the code in the loop. As you can see, MSVC does better using for loops rather than while loops in this case. The main reason being that the post increment / decrement is more costly than the pre version. So 'while (--i)' would produce a better result, you'd need to change the loop logic slightly. Even with this, the compiler is still one byte and one instruction worse than hand coded asm.

So, in the end, it pays to use pre-increment / decrement rather than post, even for PODs, and that counting down to zero is better than counting up (which is logical when you consider the way the x86 instructions work) and that hand coding asm doesn't save a great deal.

Of course, this only applies to MSVC6, your compiler may do things differently.

Skizz

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement