info: loop for - 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: info: loop for (
/showthread.php?tid=369241)
info: loop for -
NewbieScripter - 16.08.2012
what changes from
pawn Код:
for(new i = 0; i < GetMaxPlayers(); i++)
to
pawn Код:
for(new i = 0; i < MAX_PLAYERS; i++)
?
Re: info: loop for -
Ranama - 16.08.2012
I don't really know but i think it's like this:
MAX_PLAYERS always = 500;
GetMaxPlayers(); Returns the value of max players in your server config.
or they are the same
Don't really know but think it's something like that
Re: info: loop for -
AndreT - 16.08.2012
The first loop you posted (for(new i = 0; i < GetMaxPlayers(); i++)) is slower than for(new i = 0; i < MAX_PLAYERS; i++)), because it has to call the GetMaxPlayers() function not once, but a lot more.
Generally, for loop speeds, I'd say the fastest is this:
pawn Код:
for(new i = 0; i != MAX_PLAYERS; i++)
// However, on top of you script, do this:
#undef MAX_PLAYERS
#define MAX_PLAYERS x
// where x is the amount of slots your server has
Then comes this:
pawn Код:
for(new i = 0, slots = GetMaxPlayers(); i != slots; i++)
And then the one that calls GetMaxPlayers every iteration:
pawn Код:
for(new i = 0; i != GetMaxPlayers(); i++)
However, for the fastest player loops (in most cases), I'd recommend that you look up the foreach include by ******. It uses a system alike linked lists.
Re: info: loop for -
NewbieScripter - 16.08.2012
Quote:
Originally Posted by AndreT
The first loop you posted (for(new i = 0; i < GetMaxPlayers(); i++)) is slower than for(new i = 0; i < MAX_PLAYERS; i++)), because it has to call the GetMaxPlayers() function not once, but a lot more.
Generally, for loop speeds, I'd say the fastest is this:
pawn Код:
for(new i = 0; i != MAX_PLAYERS; i++) // However, on top of you script, do this: #undef MAX_PLAYERS #define MAX_PLAYERS x // where x is the amount of slots your server has
Then comes this:
pawn Код:
for(new i = 0, slots = GetMaxPlayers(); i != slots; i++)
And then the one that calls GetMaxPlayers every iteration:
pawn Код:
for(new i = 0; i != GetMaxPlayers(); i++)
However, for the fastest player loops (in most cases), I'd recommend that you look up the foreach include by ******. It uses a system alike linked lists.
|
Thanks