Jump to content
Sign in to follow this  
barbolani

Performance: Multiple Paralell Loops vs One big loop

Recommended Posts

Hi!

 

As in my environment is difficult sometimes to make performace tests I ask the wise guys.

 

I have some soldier init script, which makes 1 loop to check if he's fleeing and if so, do some captive stuff.

 

My question is, what's better, one script per unit (lets say 200 scripts) with this?

 

waitUntil {sleep 1; fleeing _unit};

 

or one script server sided with this:

 

while {true} do

{

sleep 1;

{

if (fleeing _x) then {do stuff}

} forEach allunits;

};

 

Thanks in advance.

Share this post


Link to post
Share on other sites

Very useful. Thanks.

 

Every day I spend on code optimisation I come up with the same conclusion in the end: Being the worst coder in the world isEqualTo  spawning 8 units :)

 

A fleeing eventhandler would be useful anyway. No one has asked for it?

Share this post


Link to post
Share on other sites

There are some very minor differences:

foreach allunits goes through all units always, instead of the ones actually need to "flee". You need to check which units it applies too.

The one loop runs the check in one "batch", depending on the "do stuff" and whether it is called or spwned it may have some bottleneck buildup.

 

If it's only one check for a variable state, I'd say whichever is easier for you to handle .

 

In my mission I have one big while and that controls all such checks so that they don't interfere with each other. 

Share this post


Link to post
Share on other sites

forEach loops are less optimised than for loops. If the array you are checking is small, the difference is only points of a microsecond, however for large arrays the difference can increase exponentially.

I recommend using a for loop, for example:

while {true} do {
   _allUnits = allUnits;
   for "_i" from 0 to ((count _allUnits) - 1) step 1 do {
      _unit = _allUnits select _i;
      if (fleeing _unit) then {
         // do something
      };
   };
   // Delay
   sleep 1;
};

Code not tested

 

Hope this helps,

 

Bull

Share this post


Link to post
Share on other sites

Bull, what do you mean under "forEach loops are less optimised than for loops", and where did you get this info? I am just curious, and asking.

Share this post


Link to post
Share on other sites

Bull, what do you mean under "forEach loops are less optimised than for loops", and where did you get this info? I am just curious, and asking.

 

There is very little in it but there is a slight difference (you can test it using in-game functions from the debug console that measure script execution time), there is information the code optimization page. I'm not entirely sure why it does this (guessing it has something to do with forEach executing in blocks), but from testing in the past (Arma 2 and 3) forEach has (for me) always been the slower of the two when looping through large arrays.

Share this post


Link to post
Share on other sites

Thanks all.

 

From my understanding the difference is very very small, not noticeable.

 

I am trying to get rid of any waitUntil, while etc.. loop in Antistasi and I was worried about spawning one everytime an enemy spawns. But it seems there is no big difference between both methods and the only (myabe) noticeable thing is to just use eventHandlers...

Share this post


Link to post
Share on other sites

All else being equal, less threads = less performance burden of your code.

 

Also there is the fact that each thread which takes to long to execute (for example because it uses a while with sleep 1 inside) get's called eah frame, and suspended if it takes more then x amount of time.

Therefor for example 300 threads require more engine work to be done then 1 main thread which loops everything.

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  

×