for() vs foreach()
#1

Hey, guys!

Can you tell me which loop is faster for() or foreach()?
Reply
#2

I think it doesn't really matter. Even you do an itteration of 20000 you will probably only notice a difference of 1 or 2 milliseconds.

Correct me if I'm wrong, but basically doing

pawn Code:
foreach(Player, i)
{
    // Do something here if they're connected
}
is the same as

pawn Code:
for(new i; i < MAX_PLAYERS; i++)
{
    if(!IsPlayerConnected(i) || IsPlayerNPC(i)) continue;
    // Do something here if they're connected
}
Reply
#3

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

Quote:
Originally Posted by CaHbKo
View Post
'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.
No, it's not forced to go through the given amount, if you edit inside the loop, for example:

pawn Code:
for(new i; i < MAX_PLAYERS; i ++ ) {
     if(!IsPlayerConnected(i)) continue;
}
Reply
#5

Quote:
Originally Posted by Carlton
View Post
No, it's not forced to go through the given amount, if you edit inside the loop, for example:

[CODE]
But it still checks 500 times if i is smaller than MAX_PLAYERS and calls 500 times IsPlayerConnected :/

Quote:
Originally Posted by Vince
View Post
Correct me if I'm wrong, but basically doing

[CODE]

is the same as

[CODE]
Basically yes but its like race, if foreach would be a person he would only drive so much how he needs but for would drive till his vehicle stops and that each time

And if you have a timer each second ... that would be a lot or rounds which werent necessary
Reply
#6

So you are saying that 'foreach' is faster but not soo much faster. I've tried to find out how to use 'foreach' but I just don't get it. Can someone explain me how should I use it?
Reply
#7

for the normal loop replacer using:

pawn Code:
for(new i = 0; i < MAX_PLAYERS; i++)
you can do:
pawn Code:
foreach(Player, i)
< note, if you use this, you don't need the IsPlayerConnected since it checks.

But, there is wayyy more features you can do than just loop players. Check out the main foreach topic!
Reply
#8

This is based on the assumption that both loop methods are to be used for players, when in reality they can be used for a lot of purposes.

Quick Analysis:
In general, foreach vs. for is simply a memory vs. time argument.

foreach creates an extra array behind the scenes (a linked list), the loop will loop n times (n = "Current Number of Players": dynamic). It does 1 check per iteration, this check uses data within the Pawn script.

for does not create an extra array, however it will ALWAYS loop n times (n = "Max Players": constant).. It does a constant 2 checks per iteration. One of the checks involves a call to a native function rather than a 'pure' value check of data already inside the Pawn script.

Summary:
  • foreach is faster, uses more memory and is easier to use.
  • for is slower, uses less memory but is more difficult to use.
Conclusion:
The extra memory used in foreach is generally taken advantage of multiple times due to the amount of times a "player loop" is usually required by a script. For this reason it's recommended to use foreach in general purposes, the extra memory is insignificant to most servers.
Reply
#9

So it's ok to use for? It won't be a big diffrence if I use foreach?
Reply
#10

Foreach is faster. All of you guys are WRONG. Foreach doesn't loop through all players and check if they are online.

When a player connects it adds their id into an Iteration. Foreach loops through the iteration of connected id's.

Example:
Code:
id 0 connected
id 5 connected
id 99 connected
It wont loop through all 100 players checking if 0,5,99 are connected. Since it is in the interation, it will only pass through the id's: 0, 5, and 99. Nothing else.

If you have two players in the server with id 40 and 60, it wont loop through numbers 1-60, it will only output id 40, and 60.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)