AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   different client looping? (https://forums.alliedmods.net/showthread.php?t=278764)

nergal 02-07-2016 16:18

different client looping?
 
so i've noticed that alot of code just loops through using a simple for loop

PHP Code:

//either
for (int client 1client <= MaxClientsclient++)
{
//code
}

//or
for (int client 1client <= MaxClients; ++client)
{
//code


question is, why not use something like this?

PHP Code:

int client;
while ( ++
client <= MaxClients )
{
//code


isn't the above code essentially the same but shorter?

Also, does it make any difference in execution speed if I use the loop in reverse?

PHP Code:

int client MaxClients+1;
while ( --
client )
{
//code



Kailo 02-07-2016 17:57

Re: different client looping?
 
It's the same (just syntax simplification). At compile time
PHP Code:

for (int client 1client <= MaxClientsclient++)
{
//code


will convert to
PHP Code:

int client 1;
while (
client <= MaxClients)
{
//code
client++;


No difference with reverse loop (only order of indexes).

Neuro Toxin 02-07-2016 18:07

Re: different client looping?
 
I wouldn't call it shorter.

It has one extra line!

nergal 02-09-2016 01:16

Re: different client looping?
 
Quote:

Originally Posted by Neuro Toxin (Post 2390974)
I wouldn't call it shorter.

It has one extra line!

if that's the case, then what's wrong with this?

PHP Code:

for (int client 0; ++client <= MaxClients;)
{
//code



nosoop 02-09-2016 02:53

Re: different client looping?
 
There could be a point where simplifying ends up making things less readable. Everyone with a basic knowledge of for loops can make sense of "declare a variable, check if value is within bounds, increment", but some newer users might take a bit more time to understand "declare a variable, check the prefix incremented value (what does that even mean?), why is there nothing in the third spot".

For the while-loop in the first post, it's a case of scope; client will continue to be available outside of the loop. Personal opinion, but it just seems cleaner to have a variable declared in the scope of the loop rather than worry about it if you use multiple loops in one function (you'd have to declare a new variable or reinitialize an existing one for each).

Regardless, we could use any of those variations; it's just different.

Akuba 02-09-2016 07:24

Re: different client looping?
 
I use for loops for clients because I'm used to them and know they work. I never bothered myself with finding a new way to do these.

Anyways, I wonder how Sourcepawn handels preincrement and postincrement.

Mitchell 02-09-2016 12:11

Re: different client looping?
 
I seen people who loop through clients like so
for(new i = MaxClients+1; i>0; i--)
Which is weird, but essentially covers all players just like incremental.

nergal 02-09-2016 15:14

Re: different client looping?
 
Quote:

Originally Posted by Mitchell (Post 2391446)
I seen people who loop through clients like so
for(new i = MaxClients+1; i>0; i--)
Which is weird, but essentially covers all players just like incremental.

is there even a performance difference between incrementing of decrementing?

plus, is there more performance speed to pre or post inc/dec-rement?

klippy 02-09-2016 15:51

Re: different client looping?
 
Quote:

Originally Posted by nergal (Post 2391498)
is there even a performance difference between incrementing of decrementing?

plus, is there more performance speed to pre or post inc/dec-rement?

There should be no difference in it, they are both 1 simple instruction.

And by the way, it's probably the best to do:
Code:
i != 0 // in the loop Mitchell previously wrote // instead of i <= MaxPlayers

First case just loads a variable and generates "jump if 0" instruction, which is better than second case because it loads the first variable from the stack, the second one from data and then generates "jump if greater" instruction.
Anyway, we are talking here about micro optimizations.

Powerlord 02-09-2016 15:54

Re: different client looping?
 
Quote:

Originally Posted by Kailo (Post 2390968)
It's the same (just syntax simplification). At compile time
PHP Code:

for (int client 1client <= MaxClientsclient++)
{
//code


will convert to
PHP Code:

int client 1;
while (
client <= MaxClients)
{
//code
client++;


No difference with reverse loop (only order of indexes).

They aren't quite identical. In the for loop, client is only in scope inside the for loop.


All times are GMT -4. The time now is 22:34.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.