Jump to content

While or For - Which loop works hardest?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
15 replies to this topic

#1
shibbythestoner

shibbythestoner

    Programmer

  • Members
  • PipPipPipPip
  • 135 posts
I've seen arguments before about this, do for loops require most work (more CPU, memory, etc) on the computer's part, or are whiles more stressing?
For example, to make an infinite loop
for(;;){}
while(1){}
Or perhaps for a better example
for(int i=1;i<21;i++){
//statements
}
//or
int i=1;
while(i<21){
i++;
}

Posted Image

#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Generally, I use For loops when I know the amount of times I want to loop. I use while loops when I want an event to trigger an end (such as a boolean value). If you are going to be counting a variable it makes more sense to use a for loop.

One way to determine which is fast would be to benchmark them. With your example above, my vote goes with the for loop.

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
The answer will depend a lot on the compiler and compiler options used. Some compilers may create identical code from a for loop and a while loop (for loops are just a different syntax for while loops, after all). Some compilers may only produce the same code for certain levels of optimization. Some compilers may never produce the same code and be biased one way or the other. Personally, I use the form of loop that most clearly expresses the thought I have for the loop's purpose.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
shibbythestoner

shibbythestoner

    Programmer

  • Members
  • PipPipPipPip
  • 135 posts
I agree that it's best just to loop using the most suitable method. Interesting about different compilers coding it a different way.
Posted Image

#5
RichardM

RichardM

    Newbie

  • Members
  • PipPip
  • 21 posts
If you know ahead of time exactly how many loops to execute, a for loop is better for keeping clear what is going on.

However, if you want to enhance execution speed, and the loop itself is small, you could turn the for loop into a reverse while loop, which should take about 1/3 the time to execute.

Another way to speed up program execution is to use a for loop with loop unrolling. However, like the reverse while loop, the code becomes less readable; if you come back to it after setting it aside for six months, you might not understand it.

#6
shibbythestoner

shibbythestoner

    Programmer

  • Members
  • PipPipPipPip
  • 135 posts

RichardM said:

If you know ahead of time exactly how many loops to execute, a for loop is better for keeping clear what is going on.

However, if you want to enhance execution speed, and the loop itself is small, you could turn the for loop into a reverse while loop, which should take about 1/3 the time to execute.

Another way to speed up program execution is to use a for loop with loop unrolling. However, like the reverse while loop, the code becomes less readable; if you come back to it after setting it aside for six months, you might not understand it.

Good points there. Thanks.
Posted Image

#7
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
There is also the do loop:
do{}while(1);


#8
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
The do while loop is used if you want a piece of code to execute at LEAST once. I like both loops but what Jordan said is right.

For loops you can set how many times you want it to repeat, while loops are used if you don't know how many times you want it to execute.
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation

Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!


#9
outsid3r

outsid3r

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 623 posts
I have generated an assembly output file via turbo c++ of both while and for, and the results are this:

For infinite for:


@1@30:

	jmp	short @1@30


For infinite while:


@1@30:

	jmp	short @1@30


All equal as expected, now for an iteration between 0 and 10:


For loop:


             push	si

	xor	si,si

	jmp	short @1@86

@1@58:

	inc	si

@1@86:

	cmp	si,10

	jl	short @1@58




While loop:


             push	si

	xor	si,si

	jmp	short @1@58

@1@58:	

	mov	ax,si

	inc	si

	cmp	ax,10

	jl	short @1@58



In this case, the while loop takes one more instruction, "mov ax, si", so the for is more faster in this case with this compiler, however, a better compiler would not generate that instruction because its unseless, it can be exactly like for, so i think that in new compilers probably its just the same using for or while.

#10
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
What level of optimization did you use when you compiled it?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#11
outsid3r

outsid3r

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 623 posts
i used default. however if you see that code can't be more optimised than that, exept for while.

#12
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Depending on the compiler, default optimization may go for a "literal" translation of the code, while even a modest optimization will shift them to align with each other. This is all compiler dependent, of course.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog