GetMaxPlayers() > MAX_PLAYERS ? -
Desert - 30.09.2010
GetMaxPlayers is a unused, maybe even forgotten function. Instead of it, we use the good old MAX_PLAYERS. MAX_PLAYERS, who after the release of 0.3, has been defined as 500. Because 500 is the max ammount of players a server can have. MAX_PLAYERS is most found in variables and loops. And it's a well known fact amongst experienced SA-MP scripters, that these 2 elements is very good at eating server CPU. Therefor we do with all our might tell the new people, that they need to be careful. Like we often tell them, that strings shouldn't be 256. But when we give them help, and show them some code. We give them a loop that could look like this:
pawn Код:
for(new i=0; i<MAX_PLAYERS; i++)
{
if(IsPlayerConnected(i))
{
Anyone who has worked with loops, can tell that this will go from 0 to 1 to 2 to 3 and all the way up to 499. 0 to 499 is a long and not a very suffecient way to go. It will simply eat your CPU.
Now let's go back to the fact, that we tell players NOT to use a 256 cells strings, because it's very bad for your server performance. Take a script, and say that the guy who made it have learned not to use 256 cells strings. He optimizes hes server, and feels happy about it. He just cut half of all hes 40 strings. This saved him an amazing count of 20480 bytes! According to my small calculation of 128(cells saved)*40(strings)*4(bytes). new something[MAX_PLAYERS] works the same way, as a string. exept we use the cells diffrently. Instead of saving a letter on each we save a number on each. The happy guy, who just saved 20480 bytes. Is the owner of a small popular server with 40 players. Let's say he haves 50 global variables, using MAX_PLAYERS. This gives a byte use of 100000 bytes. Where he only needs 8000 of them. The rest of the many bytes, is wasted. I hope you get the idea of that using MAX_PLAYERS will (in almost any cases) be WAY to many. Most big servers only haves 100 slots! Therefor making MAX_PLAYERS(500) into 100 will spare you from wasting 80% CPU! But instead of changing all your variables, everytime your server changes ammount of slots, or releasing insufficient code to the public. Simply because it's impossible to tell how many slots their server uses. Use GetMaxPlayers()! If you read on the wiki
https://sampwiki.blast.hk/wiki/GetMaxPlayers . You will know that this function opens the .CFG file and reads how many slots the server is on. This can save you a bunch of bytes! And it works the same way as MAX_PLAYERS.
Heres some examples with a 40 slots server:
pawn Код:
new Money[MAX_PLAYERS];
new Score[MAX_PLAYERS];
new Team[MAX_PLAYERS];
for(new i=0; i<MAX_PLAYERS; i++)
{
Score[i] = GetPlayerScore(playerid);
Money[i] = GetPlayerMoney(playerid);
Team[i] = GetPlayerTeam(playerid);
return 1;
}
This small code uses 6000, 5520 bytes more than needed, just on variables.
And the loop will go through 460 unneccesary IDs. Since PAWN is a single threaded language, it will first go through ALL the 500 numbers, before it begins on anything else. This can result in information for other players comes late. This is known as lag.
Here is a more efficient piece of code which does the same
pawn Код:
new Money[GetMaxPlayers()];
new Score[GetMaxPlayers()];
new Team[GetMaxPlayers()];
for(new i=0; i<GetMaxPlayers; i++)
{
Score[i] = GetPlayerScore(playerid);
Money[i] = GetPlayerMoney(playerid);
Team[i] = GetPlayerTeam(playerid);()
return 1;
}
This script only uses 480 bytes on variables for the 40 players server. And the loop will be finished 92% faster!
And notice how similar the 2 pieces of code really is, you don't need to learn alot of new stuff on it.
And im not 100% sure on this 1 but i think it might be a bit unefficient to open the .CFG file every time. So therefor you can just make a variable like this under Main(): new Max_Players = GetMaxPlayers();
This will use 4 bytes, but will save you from using time opening the file every time.
So therefor i come to conclusion that GetMaxPlayers() > MAX_PLAYERS in any case exept, if you actualy run a server with 500 slots.
Any feedback is welcome
Edit: Also forgot to mention that even though GetMaxPlayers() will help optimize your loops, foreach by ****** will still be the best choice for looping
Re: GetMaxPlayers() > MAX_PLAYERS ? -
willsuckformoney - 30.09.2010
Nice Tutorial, believe it or not this just gave me an idea of something to do. Thanks and good job!
Re: GetMaxPlayers() > MAX_PLAYERS ? -
[HiC]TheKiller - 30.09.2010
I vote foreach :P. Foreach beats them all
.
Re: GetMaxPlayers() > MAX_PLAYERS ? -
Desert - 30.09.2010
Quote:
Originally Posted by [HiC]TheKiller
I vote foreach :P. Foreach beats them all .
|
Yep
I also forgot to mention it, that's why i edited the post about 20 seconds after it was posted because i realised i forgot to say foreach is better. But this also works on normal player variables, where Carlton Prooved that PVar is less efficient than variables. So i still think this is rather useful
Re: GetMaxPlayers() > MAX_PLAYERS ? -
Slice - 30.09.2010
Looping with GetMaxPlayers is slower than MAX_PLAYERS. Also, you can't declare arrays with dynamic sizes.
https://sampforum.blast.hk/showthread.php?tid=65567
Edit:
There is one way you can use GetMaxPlayers, however.
pawn Код:
for ( new i = 0, l = GetMaxPlayers( ); i < l; i++ )
{
if ( IsPlayerConnected( i ) )
{
// ...
}
}
Or you could make a new global variable called g_MaxPlayers and set it to GetMaxPlayers( ) in OnGameModeInit then use that for looping.
However, I would sugget simply re-defining MAX_PLAYERS:
pawn Код:
#undef MAX_PLAYERS
#define MAX_PLAYERS (20)
Re: GetMaxPlayers() > MAX_PLAYERS ? -
[XST]O_x - 30.09.2010
edit: spam
Re: GetMaxPlayers() > MAX_PLAYERS ? -
Slice - 30.09.2010
Quote:
Originally Posted by [XST]O_x
pawn Код:
new variable[GetMaxPlayers()];
Won't happen.
You can also simply redefine your MAX_PLAYERS, as g_aSlice said, but if you know how much slots you use it's actually the best doing:
pawn Код:
#define MY_SERVER_SLOTS 70
pawn Код:
for(new i = 0; i < MY_SERVER_SLOTS+1; i++) { }
|
I then you'd have to go changing MAX_PLAYERS everywhere in your script!
Also, you don't need to do MY_SERVER_SLOTS
+1 as the highest ID on a server with 70 slots would be 69.
Re: GetMaxPlayers() > MAX_PLAYERS ? -
[XST]O_x - 30.09.2010
Quote:
Originally Posted by g_aSlice
I then you'd have to go changing MAX_PLAYERS everywhere in your script!
Also, you don't need to do MY_SERVER_SLOTS+1 as the highest ID on a server with 70 slots would be 69.
|
Haven't thought of that lol, anyway I didn't know the highest ID would be 69, never got a full server on 70 slots.
Re: GetMaxPlayers() > MAX_PLAYERS ? -
Slice - 30.09.2010
It starts from 0, not one; that's why.
Re: GetMaxPlayers() > MAX_PLAYERS ? -
LarzI - 30.09.2010
Offtopic: Why make your own macro, when you can easily undef and re-define MAX_PLAYERS?
As Y_Less said; just no..
Re: GetMaxPlayers() > MAX_PLAYERS ? -
legodude - 30.09.2010
Code:
new Money[GetMaxPlayers()];
new Score[GetMaxPlayers()];
new Team[GetMaxPlayers()];
for(new i=0; i<GetMaxPlayers; i++)
{
Score[i] = GetPlayerScore(playerid);
Money[i] = GetPlayerMoney(playerid);
Team[i] = GetPlayerTeam(playerid);
return 1;
}
little fix.
Code:
new Money[GetMaxPlayers()];
new Score[GetMaxPlayers()];
new Team[GetMaxPlayers()];
for(new i=0; i<GetMaxPlayers(); i++)//Mind the brackets after getmaxplayers here ;)
{
Score[i] = GetPlayerScore(playerid);
Money[i] = GetPlayerMoney(playerid);
Team[i] = GetPlayerTeam(playerid);
return 1;
}
Re: GetMaxPlayers() > MAX_PLAYERS ? -
[XST]O_x - 30.09.2010
@legodude
As mentioned repeatedly, you may never use a function as a variable array.
Re: GetMaxPlayers() > MAX_PLAYERS ? -
wups - 30.09.2010
Why call the same function a billion times?
pawn Code:
new max = GetMaxPlayers();
new playerMoney[max];
new score[max];
Re: GetMaxPlayers() > MAX_PLAYERS ? -
Claude - 30.09.2010
MAX_PLAYERS is slower than
GetMaxPlayers() if you are NOT talking about full servers
-
MAX_PLAYERS defines to 500 players
-
GetMaxPlayers() defines to the row of "slots" in "server.cfg" / not sure
Re: GetMaxPlayers() > MAX_PLAYERS ? -
Calgon - 30.09.2010
Quote:
Originally Posted by Claude
MAX_PLAYERS is slower than GetMaxPlayers() if you are NOT talking about full servers
- MAX_PLAYERS defines to 500 players
- GetMaxPlayers() defines to the row of "slots" in "server.cfg" / not sure
|
Can you not read at all?
Quote:
* Speed order
Different language features take different times to execute, in general the order is (from fastest to slowest):
* Nothing
* Constants (MAX_PLAYERS)
* Variables
* Arrays
* Native functions (GetMaxPlayers())
* Custom functions
* Remote functions
|
The loops execute faster with a constant being evaluated per loop, executing a function for every loop is a lot slower.
Re: GetMaxPlayers() > MAX_PLAYERS ? -
Kyosaur - 30.09.2010
Quote:
Originally Posted by wups
Why call the same function a billion times?
pawn Code:
new max = GetMaxPlayers();
new playerMoney[max]; new score[max];
|
Variables are handled during compile time, at that time there is no way to know the value of GetMaxPlayers; Which means this is invalid.
The only way to do this is to redefine MAX_PLAYERS's value to what your server.cfg says (like its been said before).
Re: GetMaxPlayers() > MAX_PLAYERS ? -
ipsBruno - 01.10.2010
Quote:
Originally Posted by Claude
MAX_PLAYERS is slower than GetMaxPlayers() if you are NOT talking about full servers
- MAX_PLAYERS defines to 500 players
- GetMaxPlayers() defines to the row of "slots" in "server.cfg" / not sure
|
NOOTT MAN
so it will not make as many checks on the function GetMaxPlayers
GetMaxPlayers = Function
MAX_PLAYERS = Macro
GetMaxPlayer is slower
If it is required to use GetMaxPlayer use the following:
for (new i = 0
, j = GetMaxPlayers () , i <j; i++)
But MAX_PLAYERS is further optimized.
make sure the player is online and the not is bot
so:
pawn Код:
#Define LoopPlayers (%0,%1) for(new 0%=0,%0 <%1,%0++) if(IsPlayerConnected (%0) && !IsPlayerNPC (%0))
OR USE FOREACH
Tutorial little wrong
Re: GetMaxPlayers() > MAX_PLAYERS ? -
The_Moddler - 01.10.2010
foreach uses MAX_PLAYERS?
Re: GetMaxPlayers() > MAX_PLAYERS ? -
LarzI - 01.10.2010
Quote:
Originally Posted by The_Moddler
foreach uses MAX_PLAYERS?
|
It's the fastest, so that's pretty obvious.
Re: GetMaxPlayers() > MAX_PLAYERS ? -
Claude - 01.10.2010
Quote:
Originally Posted by Calgon
Can you not read at all?
The loops execute faster with a constant being evaluated per loop, executing a function for every loop is a lot slower.
|
Uhm.. Stfu okay? Don't wanna get banned again? Work on your attitude
[FeK]DraKiNs @ Thanks for your explaination