SA-MP Forums Archive
Which loop is more efficient? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Which loop is more efficient? (/showthread.php?tid=322693)



Which loop is more efficient? - T0pAz - 03.03.2012

This
pawn Код:
for ( new index = 0; index != MAX_PLAYERS; ++ index )
Or this
pawn Код:
for ( new index = 0; index < MAX_PLAYERS; index ++ )
P.S. state your reason as-well.


Re: Which loop is more efficient? - Vince - 03.03.2012

I don't think it matters. Have you tested it? Pre- or post-incremention of the variable has also no effect on the statements in the loop.


Re: Which loop is more efficient? - T0pAz - 03.03.2012

Quote:
Originally Posted by Vince
Посмотреть сообщение
I don't think it matters. Have you tested it? Pre- or post-incremention of the variable has also no effect on the statements in the loop.
The result is same. But I still want to know what to use.


Re: Which loop is more efficient? - Jochemd - 03.03.2012

If it's the same, I'd use...

pawn Код:
for ( new index = 0; index < MAX_PLAYERS; index ++ )
... because it's easier to understand.


Re: Which loop is more efficient? - Babul - 03.03.2012

SECOND one indeed. if you (accidently) set the index to >MAX_PLAYERS, the loop could be infinite.
sry i've read them too fast. major fail. oops
counting from a minimum (0) to a max(500) always should exclude the chance to get larger values like 501. the != check accepts both. the < only accepts the 0-499 boundary


Re: Which loop is more efficient? - T0pAz - 03.03.2012

Quote:
Originally Posted by Babul
Посмотреть сообщение
first one indeed. if you (accidently) set the index to >MAX_PLAYERS, the loop could be infinite.
Yep. Thanks bud

Edit: Another big thanks!