SA-MP Forums Archive
fastest and most effective possible? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: fastest and most effective possible? (/showthread.php?tid=619429)



fastest and most effective possible? - Spenker - 17.10.2016

Method 1:
Код HTML:
new Iterator: WarPlayers<MAX_PLAYERS>;
foreach (new i : GunGamePlayers) 
{
	// code line, code line ...
       // etc etc etc ...
}
Method 2:
Код HTML:
new WarPlayers[MAX_PLAYERS];
for(new i = 0; i <= GetPlayerPoolSize(); i++)
{
	if(IsPlayerConnected(i) && WarPlayers == 1)
	{
		// code line, code line ...
               // etc etc etc ...
	}
}
I want to choose one method, the most efficient and fastest. Method plan to add on another 20-30 systems.


Re: fastest and most effective possible? - SickAttack - 17.10.2016

Why don't you test them to see which one is faster? Just change the second loop to: for(new i = 0, j = GetPlayerPoolSize(); i <= j; i++)


Re: fastest and most effective possible? - Spenker - 17.10.2016

Quote:
Originally Posted by SickAttack
Посмотреть сообщение
Why don't you test them to see which one is faster? Just change the second loop to: for(new i = 0, j = GetPlayerPoolSize(); i <= j; i++)
Why?
Difference?
Код HTML:
for(new i = 0, j = GetPlayerPoolSize(); i <= j; i++)
/
Код HTML:
for(new i = 0; i <= GetPlayerPoolSize(); i++)



Re: fastest and most effective possible? - SickAttack - 17.10.2016

Quote:
Originally Posted by Spenker
Посмотреть сообщение
Why?
Difference?
Код HTML:
for(new i = 0, j = GetPlayerPoolSize(); i <= j; i++)
/
Код HTML:
for(new i = 0; i <= GetPlayerPoolSize(); i++)
Because the loop won't call the function every iteration, it saves the value in a variable and uses that variable for every iteration.


Re: fastest and most effective possible? - Stinged - 17.10.2016

Quote:
Originally Posted by Spenker
Посмотреть сообщение
Why?
Difference?
Код HTML:
for(new i = 0, j = GetPlayerPoolSize(); i <= j; i++)
/
Код HTML:
for(new i = 0; i <= GetPlayerPoolSize(); i++)
First one calls GetPlayerPoolSize once, and stores its value in j.
Second one calls GetPlayerPoolSize multiple times (Depending on the value it actually outputs).

So yes, the first one is actually faster.

But to answer your main question, iterators (y_iterate/foreach) are much faster.
They only loop through the existing values, while the second one loops N times.
*N = The highest player id in the server.


Re: fastest and most effective possible? - Misiur - 17.10.2016

Iterator always will be the fastest - just remember to add and remove values from it to not get wonky stuff. You've said that you'll do it for 30 systems. I guess a player can be at most in 1 of those at once (i.e Player in GunGame can't play WarGame at the same time). If you are using latest YSI, you can reduce memory used too:
pawn Код:
enum E_GAME
{
     E_GAME_WAR,
     E_GAME_GUN,
     //etc.
}
new Iterator:GamePlayer<_:E_GAME, MAX_PLAYERS>;

//then
foreach(new playerid:GamePlayer<_:E_GAME_WAR>)
This way you'll only use E_GAME + MAX_PLAYERS cells, not E_GAME * MAX_PLAYERS.


Re: fastest and most effective possible? - Spenker - 17.10.2016

Thank you all,
300+ players server can be run (for) without lag server?
At the moment only use for, and sometimes I have a little lag. But I do not know if the problem loops. HOST is very performant.
I'll update loops on option 1. [ for(new i = 0, j = GetPlayerPoolSize(); i <= j; i++) ]


Re: fastest and most effective possible? - Spenker - 18.10.2016

Anyone?


Re: fastest and most effective possible? - SickAttack - 18.10.2016

Yes, that for loop is just fine.