Defining MAX_PLAYERS -
Eyce - 15.04.2016
I was just wondering if it would be a lot better if you redefine "MAX_PLAYERS" to the value that you want? If for example, you have a server with 500 slots but you're aware of the fact that your server wouldn't even go beyond 100 or 200 players. Would this be helpful especially with loops? Such as:
Re: Defining MAX_PLAYERS -
yugecin - 15.04.2016
Foreach only loops over the connected players, it doesn't use MAX_PLAYERS so on those loop you won't save anything. Normal for loops will obviously loop less as the value is lower, which would be a little faster.
Redefining MAX_PLAYERS will also reduce the amount of memory used, especially with arrays of playerdata.
But I wouldn't redefine my MAX_PLAYERS on 200 if I have 500 slots available without lowering the amount of slots.
Re: Defining MAX_PLAYERS -
Eyce - 15.04.2016
Quote:
Originally Posted by yugecin
Foreach only loops over the connected players, it doesn't use MAX_PLAYERS so on those loop you won't save anything. Normal for loops will obviously loop less as the value is lower, which would be a little faster.
Redefining MAX_PLAYERS will also reduce the amount of memory used, especially with arrays of playerdata.
But I wouldn't redefine my MAX_PLAYERS on 200 if I have 500 slots available without lowering the amount of slots.
|
I see. Thank you. I was about to ask a follow up question but I found all the answers that I was looking for on the link below.
https://sampwiki.blast.hk/wiki/MAX_PLAYERS
Re: Defining MAX_PLAYERS -
Vince - 15.04.2016
Quote:
Originally Posted by yugecin
Foreach [...] doesn't use MAX_PLAYERS
|
Yes it does. Internally. Foreach basically stores the id of the next connected player and if all players are connected then all these slots are filled. Redefining MAX_PLAYERS will save on RAM in all cases and it may save on CPU time in some cases.
Re: Defining MAX_PLAYERS -
yugecin - 15.04.2016
Yes, but I meant that it doesn't use it in a loop.
Re: Defining MAX_PLAYERS -
Eyce - 15.04.2016
Quote:
Originally Posted by Vince
Yes it does. Internally. Foreach basically stores the id of the next connected player and if all players are connected then all these slots are filled. Redefining MAX_PLAYERS will save on RAM in all cases and it may save on CPU time in some cases.
|
So if there are 50/100 online, foreach would only go through 0 to 50?
Re: Defining MAX_PLAYERS -
NaS - 15.04.2016
Quote:
Originally Posted by Eyce
So if there are 50/100 online, foreach would only go through 0 to 50?
|
That's the point of foreach (hence the name). But not only that simple, if there are 50 players connected, but there were 100 before, the connected IDs are not 0-50, but 50 IDs between 0 and 99, foreach will skip the empty slots and only call for the connected IDs.
A simple for loop will call all between 0 and 99 and you have to see which is connected or not.
Re: Defining MAX_PLAYERS -
AbyssMorgan - 15.04.2016
Use:
PHP код:
foreach(%1) for(new %1 = 0, p_%1 = GetPlayerPoolSize(); %1 <= p_%1; %1++)
Re: Defining MAX_PLAYERS -
Konstantinos - 15.04.2016
Quote:
Originally Posted by AbyssMorgan
Use:
PHP код:
foreach(%1) for(new %1 = 0, p_%1 = GetPlayerPoolSize(); %1 <= p_%1; %1++)
|
Foreach/y_iterate is still faster than pool size functions.
I'll give a simple example to see the difference:
- You create 1500 vehicles.
- You delete 1 to 1499 vehicles and you keep only the vehicle with ID 1500.
Using pool size will go from 1 to 1500 (1500 times) while foreach will loop ONLY 1 time.
Re: Defining MAX_PLAYERS -
AmigaBlizzard - 15.04.2016
Just looping through all available player-slots and checking if they're online is fast enough.
PHP код:
new starttime, endtime, blabla;
starttime = GetTickCount();
for (new i; i < 1000000; i++)
if (APlayerData[0][LoggedIn] == true)
blabla = 0;
endtime = GetTickCount();
printf("Total time to loop 1.000.000 times: %i milliseconds", endtime - starttime);
I used this code to test the speed for looping 1 million times and checking if a player is connected and it's executed in only 43 milliseconds on my home-pc.
This is 1000 times more than the highest populated server, which is only a 1000-player server.
Looping through 1000 players and checking their online status would only take 43 microseconds. This would be for the highest populated server.
In your case, a 500 player server, this would only take 21 microseconds.
This is negligable.
Increasing the loop to 25 million iterations takes 1 second, I doubt anyone would use such big loops in their server.
Real servers do it even faster than my home-pc.
I'm not bothering to use foreach and other stuff.
It doesn't give a noticeable speed-increase.
My own server only has 50 players, so each time I loop through all players, it would only take 2 microseconds.
To your question: redefining MAX_PLAYERS would require a recompile of your script and a restart of your server for changes to take effect.
And don't forget to adjust the value in your server.cfg file as well to match your script.
It's not a good idea to monitor your server constantly and restart the server with an adjusted MAX_PLAYERS value when your server is getting full or empty.
Just set it to the maximum you're allowed and leave it at that.