Jump to content
Sign in to follow this  
Icaruk

Which is better for max performance?

Recommended Posts

while {a == 2} do {
  bla;
  sleep 1;
};

or

while {true} do {
  if (a == 2) then {
     bla;
  };
sleep 1;
};

Share this post


Link to post
Share on other sites

Well...

Can we assume that "a" is always 2? Your example is kind of vague. With that code it depends on what "bla" does

Share this post


Link to post
Share on other sites

The performance hit of a simple loop with a 1 sec sleep is negligible. The main difference between the code you have pasted is that first one will run bla until a!=2, and will not run again after that no matter what. The second loop will run bla if a==2, stop running bla if a!=2 , but start running bla if a==2 again.

Share this post


Link to post
Share on other sites

For unknown reasons, seems conditions in a while loop are not reliable... I recommend allways a while {true} and inside of it, a condition with exitwith if no eternal loop is needed.

I noticed that in a script like this:

_pos = whatever position;

while {surfaceIsWater _pos} do

{_pos = whatever method of searching a new pos;

sleep 1};

The results of that where position in water, don't ask me why, instead of that I had to do this:

while {true} do

{if (!surfaceIsWater _pos) exitWith {};

_pos = whatever method of searching a new pos;

}

Share this post


Link to post
Share on other sites
For unknown reasons, seems conditions in a while loop are not reliable... I recommend allways a while {true} and inside of it, a condition with exitwith if no eternal loop is needed.

I noticed that in a script like this:

_pos = whatever position;

while {surfaceIsWater _pos} do

{_pos = whatever method of searching a new pos;

sleep 1};

The results of that where position in water, don't ask me why, instead of that I had to do this:

while {true} do

{if (!surfaceIsWater _pos) exitWith {};

_pos = whatever method of searching a new pos;

}

Weird, never had that happen, ever.

Share this post


Link to post
Share on other sites

I discovered this on Antistasi, I use that snippet to spawn groups in random positions around a marker.

After a few checks I noticed 1/5 times units were spawning in water.

Asked in the forums and that was the answer: no reliable. Changed to way of doing this and no water spawning since. Don't ask me why, but as performance wise it is almost the same, no worries...

Share this post


Link to post
Share on other sites

I also find the while conditions unreliable and I find that placing the if exitwith command before or after code executes is quite handy.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×