info: loop for
#1

what changes from

pawn Код:
for(new i = 0; i < GetMaxPlayers(); i++)
to

pawn Код:
for(new i = 0; i < MAX_PLAYERS; i++)
?
Reply
#2

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

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

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
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)