20.08.2010, 22:04
Hey, guys!
Can you tell me which loop is faster for() or foreach()?
Can you tell me which loop is faster for() or foreach()?
foreach(Player, i)
{
// Do something here if they're connected
}
for(new i; i < MAX_PLAYERS; i++)
{
if(!IsPlayerConnected(i) || IsPlayerNPC(i)) continue;
// Do something here if they're connected
}
'foreach' is faster.
It doesn't loop through unused thing, like other non-existing 499 players on our server, while 'for' goes through all of 500 players. |
for(new i; i < MAX_PLAYERS; i ++ ) {
if(!IsPlayerConnected(i)) continue;
}
No, it's not forced to go through the given amount, if you edit inside the loop, for example:
[CODE] |
Correct me if I'm wrong, but basically doing
[CODE] is the same as [CODE] |
for(new i = 0; i < MAX_PLAYERS; i++)
foreach(Player, i)
id 0 connected id 5 connected id 99 connected