Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

While or For - Which loop works hardest?

loop

  • Please log in to reply
15 replies to this topic

#1 shibbythestoner

shibbythestoner

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 109 posts

Posted 12 September 2008 - 09:45 AM

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++;
}

  • 0
Posted Image

#2 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 12 September 2008 - 02:02 PM

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.
  • 0

#3 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 13 September 2008 - 04:26 AM

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.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#4 shibbythestoner

shibbythestoner

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 109 posts

Posted 14 September 2008 - 08:22 AM

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

#5 RichardM

RichardM

    CC Newcomer

  • Just Joined
  • PipPip
  • 20 posts

Posted 19 October 2008 - 12:40 PM

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.
  • 0

#6 shibbythestoner

shibbythestoner

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 109 posts

Posted 19 October 2008 - 01:37 PM

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.
  • 0
Posted Image

#7 Aereshaa

Aereshaa

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 638 posts

Posted 19 October 2008 - 04:28 PM

There is also the do loop:
do{}while(1);

  • 0

#8 Brandon W

Brandon W

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2092 posts
  • Location:Ipswich, Australia
  • Programming Language:C, Java
  • Learning:Java, C++, JavaScript

Posted 25 October 2008 - 07:36 PM

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.
  • 0
I've returned...

#9 outsid3r

outsid3r

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 494 posts

Posted 08 November 2008 - 03:03 AM

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.
  • 0

#10 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 08 November 2008 - 09:14 AM

What level of optimization did you use when you compiled it?
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#11 outsid3r

outsid3r

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 494 posts

Posted 08 November 2008 - 09:47 AM

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

#12 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 08 November 2008 - 11:57 AM

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.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/






Also tagged with one or more of these keywords: loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download