The fastest loop -
Tomer!.$ - 24.04.2012
Well, I actually wonder what's faster, i < MAX_PLAYERS or creating a variable ConnectedPlayers which means that we'll also need to ++ on OnPlayerConnect and -- on OnPlayerDisconnect, so is it worth creating this variable just for faster loops?
Re: The fastest loop -
ViniBorn - 24.04.2012
I always do this...
Re: The fastest loop -
MP2 - 24.04.2012
https://sampforum.blast.hk/showthread.php?tid=92679
Re: The fastest loop -
Richie© - 24.04.2012
Use the foreach include.
https://sampforum.blast.hk/showthread.php?tid=92679
Re: The fastest loop -
MP2 - 24.04.2012
Too late \o
Re: The fastest loop -
Tomer!.$ - 24.04.2012
I don't really like downloading lots of includes to do the job for me, I prefer learning and expanding my knowledge.
Re: The fastest loop -
ViniBorn - 24.04.2012
If you don't want to use foreach, use the variable that counts the number of players.
Re: The fastest loop -
kadaradam - 24.04.2012
Undefine the MAX_PLAYERS,and define your slot amount!
pawn Код:
#undef MAX_PLAYERS
#define MAX_PLAYERS 40
or use GetMaxPlayers()
Re: The fastest loop -
MP2 - 24.04.2012
Quote:
Originally Posted by Tomer!.$
I don't really like downloading lots of includes to do the job for me, I prefer learning and expanding my knowledge.
|
foreach works like this (or at least I assume, never actually looked at the code):
slot 0: player ID 1
slot 1: player ID 8
slot 2: player ID 2
Which will result in a loop of 3. If you did it your way, you would need a loop of 9 (0-8). Using connected players will NOT work, because player slots don't shuffle down, i.e. players online might be 2, but their IDs could be 5 and 8.
Re: The fastest loop -
AndreT - 24.04.2012
There's a reason the theory in the first post wont work. You will need a variable that will store the highest player ID in such case, because IDs are not rearranged in the server if someone with a lower ID decides to reconnect. For increased efficiency, as others already pointed out, why not use the foreach solution by ******? It can be fairly useful for other things in your script as well, since it handles linked lists.
The slowest way to do your loops would be:
pawn Код:
for(new i = 0; i != GetMaxPlayers(); i++)
Since this will call GetMaxPlayers() exactly the amount of players allowed on your server. And as we know, calling even native funcs like this, in comparison to other methods, is time costly.
If you don't have the chance to determine a valid MAX_PLAYERS in the code, then creating an extra variable will help. You can do this prior to the loop or even load the value in OnGameModeInit/OnFilterScriptInit or whatever is called when your library loads.
pawn Код:
for(new i = 0, j = GetMaxPlayers(); i != j; i++)
And then the best solution for people that know the amount of their slots.
pawn Код:
for(new i = 0; i != MAX_PLAYERS; i++)
Just remember to redefine your MAX_PLAYERS as kadaradam showed above:
pawn Код:
#undef MAX_PLAYERS
#define MAX_PLAYERS 100
Also, keep in mind that when you want to make your loops faster, there are numerous things that people often oversee. Imagine a system where there's something like:
pawn Код:
#define NO_MINIGAME 1
#define MINIGAME_FALLOUT 2
public OnPlayerConnect(playerid)
{
pState[playerid] = NO_MINIGAME;
}
public OnPlayerDisconnect(playerid)
{
pState[playerid] = 0;
}
You'll notice that for a connected player, pState is never 0. You can make your loops faster when they look like this:
pawn Код:
for(new i = 0; i != MAX_PLAYERS; i++)
{
if(IsPlayerConnected(i) && pState[i] == MINIGAME_FALLOUT)
{
}
}
Now an easy thing to consider: can pState be MINIGAME_FALLOUT for a not connected player slot? Nope. This makes calling IsPlayerConnected unnecessary, since pState[playerid] == MINIGAME_FALLOUT check already involves checking if: 1. player is connected, 2. player is in the minigame!
This can be simplified to:
pawn Код:
for(new i = 0; i != MAX_PLAYERS; i++)
{
if(pState[i] == MINIGAME_FALLOUT)
{
}
}
It happens regularly to me that when I go through some of my code that's perhaps even a year old in my gamemode, I find things like this that could use theoretical improvement. I use foreach though, so not on player loops.