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

#13 RobotGymnast

RobotGymnast

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 132 posts

Posted 07 January 2009 - 02:15 PM

Don't forget that a for loop brings the incremented variable out of scope once it completes. If you care more about CPU, then you want a while (destroying objects takes CPU cycles); but if you want to have less temp variables floating around or just want less memory consumption, use a for.
  • 0

#14 Aereshaa

Aereshaa

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 638 posts

Posted 07 January 2009 - 03:21 PM

That's only true for C99 and C++.
  • 0
Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
"When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz

#15 RobotGymnast

RobotGymnast

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 132 posts

Posted 07 January 2009 - 05:05 PM

In terms of actual compiled work they look almost identical, there's a condition checked and an act performed on it.

It's true that depending on your compiler it will change, though. A for loop might use a register to store the temporary variable, and therefore it will be quick and much better. The difference is really in use: if you are going to use something that emulates a for loop (creating a variable, incrementing, etc.) then use a for loop (compilers will almost certainly optimize it better than you)
  • 0

#16 Aereshaa

Aereshaa

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 638 posts

Posted 07 January 2009 - 07:35 PM

I mean that in c89 this:
for(int i, i>len, i++){/* ... */}
Is wrong.
  • 0
Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
"When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz





Also tagged with one or more of these keywords: loop

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