For Loop vs While Loop

G

gamedev513

Guest
Hey everyone,

In my game I have a basic loop that runs through each room of my game and sets the given view/port values. Once the loop reaches the final room of the game, I end the loop. I am currently using a While Loop to handle this,however, I am curious which Loop method is faster: For Loop or While Loop? And if For is in fact faster, how would I convert the following code below into For Loop format?

Thanks!

Anthony

Code:
i = true;
rm = room_next(room);

while (i)
    {
        room_set_view(rm, 0, true, 0, 0, view_w, view_h, 0, 0, view_w, view_h, 64, 64, -1, -1, noone);
        room_set_view_enabled(rm, true);
        if (rm == room_last)
        {
            i = false;
            room_goto_next();
            break;
        }
        else
        {
            rm = room_next(rm);
        }
    }
 
S

Snail Man

Guest
Neither is faster than he other, they're just used for different things. A while loop is used when you don't know how many loops you need, and a for loop is used when you know exactly how many loops you need, be it a constant or a variable. In this case, you take the right approach
 
M

McWolke

Guest
Hey, all Loops are the same. None of them is faster.
For-loops are used when you already know how often you want to loop trough.
While-loops are used when you are not sure how often you want to loop or if you even have to start the loop.
Do-loops are used like While-loops, except that do-loops will always run at least once.
repeat-loops are used if you already know exactly how often want it to loop and not make it variable.
 
R

rui.rosario

Guest
Hey, all Loops are the same. None of them is faster.
For-loops are used when you already know how often you want to loop trough.
While-loops are used when you are not sure how often you want to loop or if you even have to start the loop.
Do-loops are used like While-loops, except that do-loops will always run at least once.
repeat-loops are used if you already know exactly how often want it to loop and not make it variable.
I actually disagree. For example, if you know you have to loop 5 times but the logic itself doesn't depend on the number of the current iteration then a repeat loop will be faster than the equivalent for loop, although most people would use the for loop.
 

BLang

Member
While it might sound like that, I'm pretty sure that a repeat loop is just a hidden for loop, so I doubt there's much of a speed difference.
 
R

rui.rosario

Guest
While it might sound like that, I'm pretty sure that a repeat loop is just a hidden for loop, so I doubt there's much of a speed difference.
I actually remember reading on the old GMC that that was not the case, in fact the repeat was faster.

And I can understand why, since in C / C++ a repeat loop could be translated into
Code:
while (i--) {

}
Instead of the equivalent for loop.
 

BLang

Member
Oh, nice, I didn't think of it that way!
I rarely ever have a use for it, though, and even less so when coding in c++... :/
 
P

Paolo Mazzon

Guest
I recall my computer teacher talking about the differences and use cases. One isn't faster than the other, and a while loop is arguably more flexible, but the main point he made was "if you plan on changing the value of [the iterator] then use a while loop; never change the value of [the iterator] in a for loop."

McWolke does a decent job explaining the types of loops, but I tend to staw away from repeat since it's basically the exact same as a for loop but doesn't give you access to the iterator.
 
F

Fuzenrad

Guest
Makes no difference, if your loop is crashing the game (or low fps), probably its because a something wrong and is giving a lot more turns than should.
 
R

rui.rosario

Guest
Makes no difference, if your loop is crashing the game (or low fps), probably its because a something wrong and is giving a lot more turns than should.
Agreed, I was just pointing out that someone in the old GMC (can't recall who or where) stated that repeat loops were faster than for loops. But if they're fundamentally wrong it's not one over the other that will fix it.
 
G

gamedev513

Guest
Awesome! thanks for all the help. I guess for now I will assume the code I'm currently using is good how it is. Just wanted to get a second opinion(s) before I move on.
 
A repeat loop is faster then a for loop only if there is not iterator being used. Back in the GM8 days, repeat loops were always faster then for loops, but Studio code has been optimized so that for loops can now beat out repeats with iterators. I'm fairly certain this was directly from either Mike or Russell, but personally, my own speed tests have never confirmed this, and I'm still stuck on using repeats for everything, cause I'm a silly beast :p
 
F

Fuzenrad

Guest
Agreed, I was just pointing out that someone in the old GMC (can't recall who or where) stated that repeat loops were faster than for loops. But if they're fundamentally wrong it's not one over the other that will fix it.
In any case, hardly a complex situation need a faster loop method. It depends on the skill of the programmer. :p
A repeat loop is faster then a for loop only if there is not iterator being used.
Well, i particularly never comprove this, in my tests always i had the same results (at the same time).. I know if a syntax error in while tends to crash the game much more than an error in for.
 
I've also never been able to get definitive results on any tests, not in Studio anyway. I'm just stubborn and set in my ways like an old man :p Personally, I think the difference between them all is negligible and one shouldn't concern themselves with that unless they really need to squeeze the engine for every micron of juice. "Premature Micro-optimization is the root of all Evil!" ... Or so they say...
 

GMWolf

aka fel666
If using the YYC, then the all loops are pretty much equivalent in speed as they probably will all be compiled down to the same code.

However, if using the runner, the key is reducing the number of calls and assignments.
So the repeat loop ends up being the fastest, but not very versatile.
While and for are about equivalent.
 
S

seanm

Guest
You can do loop unrolling as well to speed up your code

Code:
for(i=0;i<100;i+=5)
{
    do
    do[i+1]
    do[i+2]
    do[i+3]
    do[i+4]
}
This is due to the way the code gets handled when its converted to assembly.
Returning to the top of a loop takes a lot of instructions, so by executing multiple actions per loop, you can seriously speed up your code. (sometimes).
 
You can do loop unrolling as well to speed up your code

Code:
for(i=0;i<100;i+=5)
{
    do
    do[i+1]
    do[i+2]
    do[i+3]
    do[i+4]
}
This is due to the way the code gets handled when its converted to assembly.
Returning to the top of a loop takes a lot of instructions, so by executing multiple actions per loop, you can seriously speed up your code. (sometimes).
If you need to optimize as far as the assembly level, you are doing something wrong; whether that something is using Game Maker for your engine or having generally terrible code. In modern game development, a vast majority of the time you won't have to worry about how many cycles something costs you--especially for 2D games. Don't bother over-optimizing for minute speed increases. Look for places where you can get large speed benefits and optimize there.
 
S

seanm

Guest
I disagree, there are plenty of reasons why you would want to do something like this.
World generation and chunk loading come to mind.


Edit: Though, GM probably does enough to optimize behind the scenes if you are using YYC
 
I disagree, there are plenty of reasons why you would want to do something like this.
World generation and chunk loading come to mind.


Edit: Though, GM probably does enough to optimize behind the scenes if you are using YYC
I said "a vast majority of the time". CPU-/RAM-intensive operations like chunk loading and random worldgen are some of the exceptions, obviously.
Not optimizing things that don't need to be optimized cuts down on development time by a significant amount. Having a game finished is more important than an extra ~100 FPS when fps_real is >1k. If you're making a 3D game or a Terraria-esque game, optimization is going to be more important there than when you're making a Puzzle game or a Metroidvania or an RPG.
 
S

seanm

Guest
Yeah, so I just tested it.

On the normal windows build I got these times:
  • Normal: 16 seconds
  • Unrolled: 6 seconds
On the YYC windows build I got these times:
  • Normal: .3 seconds
  • Unrolled: .2 seconds
So it looks like the YYC is doing some loop unrolling behind the scenes, as well as basically speeding everything else up 30x lol

So to optimize your game just use the YYC :p
 
P

Paolo Mazzon

Guest
...
If you're making a 3D game or a Terraria-esque game, optimization is going to be more important there than when you're making a Puzzle game or a Metroidvania or an RPG.
I disagree. Just because a puzzle type game or whatever generally requires less cpu to run, I wouldn't try to make a point of doing inefficient things for the hell of it. Optimization is always important. On the mobile scene it's really important to optimize even simple games, since the easier it is to run, the less battery you end up using. (Run Xcom on your phone for 10 minutes and compare that to 10 minutes of Candy Crush)
 
I disagree. Just because a puzzle type game or whatever generally requires less cpu to run, I wouldn't try to make a point of doing inefficient things for the hell of it. Optimization is always important. On the mobile scene it's really important to optimize even simple games, since the easier it is to run, the less battery you end up using. (Run Xcom on your phone for 10 minutes and compare that to 10 minutes of Candy Crush)
Mobile development is an exception and requires a completely different mindset to PC/Console development.

I don't recall ever saying to do wildly inefficient things purposely. The topic was about whether for loops are faster or slower than while loops. If your game needed one of those two, 99% of the time it wouldn't matter which one you actually used. The game's going to run generally the same speed, draw generally the same power on mobile devices, and with zero noticable end-user difference.
 
A

Aleksandar Gavrilovic

Guest
all this worrying about low-level optimisation is why i switched to GM in the first place :D
that said, i have yet to make a gamemaker game which has the fps bottlenecked by something other than switching the vertex batches.
 
Top