Defining MAX_PLAYERS
#1

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:
pawn Код:
foreach(Player,i) {

}
Reply
#2

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.
Reply
#3

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
Reply
#4

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.
Reply
#5

Yes, but I meant that it doesn't use it in a loop.
Reply
#6

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?
Reply
#7

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.
Reply
#8

Use:
PHP код:
foreach(%1)        for(new %0p_%GetPlayerPoolSize(); %<= p_%1; %1++) 
Reply
#9

Quote:
Originally Posted by AbyssMorgan
Посмотреть сообщение
Use:
PHP код:
foreach(%1)        for(new %0p_%GetPlayerPoolSize(); %<= 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.
Reply
#10

Just looping through all available player-slots and checking if they're online is fast enough.

PHP код:
    new starttimeendtimeblabla;
    
starttime GetTickCount();
    for (new 
i1000000i++)
        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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)