Jump to content
Robustcolor

opinion about findif vs count

Recommended Posts

Hi, what is your opinion about using findif vs count in context like these below. When is findif better to use vs the count command?

waitUntil {uiSleep 300; ({_x distance2d _veh < 1000} count allPlayers == 0)};
vs
waitUntil {uiSleep 300; allPlayers findIf {(_x distance2d _veh) < 1000} ==-1};

Here count seems to be slightly faster.           
waitUntil {uiSleep 5; (allPlayers findIf {(_x distance2d _marker) > 450} !=-1)};
vs
waitUntil {uiSleep 5; ({_x distance2d _marker < 450} count allPlayers == 0)};
                                                     
Here findIf seems slighlty faster.
while {{alive _x} count (units _group) > 0} do {};
vs
while {units _group findIf {alive _x} >-1} do {};

count slighlty faster.

 

Share this post


Link to post
Share on other sites

Use commands for their intended purpose.

findIf when you need to find the first array element matching the criteria, count if you need to find how many elements are matching the criteria.

Performance should be a concern if you actually run into performance issues (and I'm sure those will not be solved by switching from count to findIf or vice versa).

 

Cheers

  • Like 2

Share this post


Link to post
Share on other sites

Thanks @Grumpy Old Man

The thing is i read here https://community.bistudio.com/wiki/Code_Optimisation that findIf could be slighlty faster then count depending on how you use it.

 

_someoneIsNear = (allUnits findIf { _x  distance [0,0,0] < 1000 }) != -1;		// 0.0275 ms
_someoneIsNear = { _x distance [0,0,0] < 1000 } count allUnits > 0;				// 0.0645 ms

 

Share this post


Link to post
Share on other sites

Not an opinion but you can analyse:

what's difference between allUnits and allPlayers during your test?  I guess allPlayers was a single unit array, so not really significant for such test.

and, cherry on the cake, you're running the code every 5 or even 300 seconds... We are far from an on each framed code.

Share this post


Link to post
Share on other sites
4 hours ago, Robustcolor said:

Thanks @Grumpy Old Man

The thing is i read here https://community.bistudio.com/wiki/Code_Optimisation that findIf could be slighlty faster then count depending on how you use it.

 


_someoneIsNear = (allUnits findIf { _x  distance [0,0,0] < 1000 }) != -1;		// 0.0275 ms
_someoneIsNear = { _x distance [0,0,0] < 1000 } count allUnits > 0;				// 0.0645 ms

 

 

Like Grumpy said, the most important thing is why you use it. FindIf stops the evaluation as soon as a matching result is found, whereas count evaluates all elements in an array. On small arrays it won't make a big difference, and not in a reliable way; but if you don't need to actually iterate through all elements, stick with findIf. One thing to keep in mind : the more elements matching the evaluated criteria, the more you have to gain by using findIf. It can be fast too if used to "find a needle in a haystack", but you're basically relying on luck in such a scenario (and hoping the match doesn't happen to be the last element in your array).

  • Like 3

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

×