20.08.2010, 23:51
(
Last edited by Simon; 21/08/2010 at 12:56 AM.
Reason: Added assumption I was using.
)
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:
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.
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.
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.