Raised This Month: $ Target: $400
 0% 

different client looping?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
nergal
Veteran Member
Join Date: Apr 2012
Old 02-07-2016 , 16:18   different client looping?
Reply With Quote #1

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

__________________

Last edited by nergal; 02-07-2016 at 16:46.
nergal is offline
Kailo
Senior Member
Join Date: Sep 2014
Location: Moscow, Russia
Old 02-07-2016 , 17:57   Re: different client looping?
Reply With Quote #2

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).

Last edited by Kailo; 02-07-2016 at 17:58.
Kailo is offline
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 02-07-2016 , 18:07   Re: different client looping?
Reply With Quote #3

I wouldn't call it shorter.

It has one extra line!
__________________
Neuro Toxin is offline
nergal
Veteran Member
Join Date: Apr 2012
Old 02-09-2016 , 01:16   Re: different client looping?
Reply With Quote #4

Quote:
Originally Posted by Neuro Toxin View Post
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

__________________
nergal is offline
nosoop
Veteran Member
Join Date: Aug 2014
Old 02-09-2016 , 02:53   Re: different client looping?
Reply With Quote #5

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.
__________________
I do TF2, TF2 servers, and TF2 plugins.
I don't do DMs over Discord -- PM me on the forums regarding inquiries.
AlliedModders Releases / Github / TF2 Server / Donate (BTC / BCH / coffee)
nosoop is offline
Akuba
Senior Member
Join Date: Oct 2013
Old 02-09-2016 , 07:24   Re: different client looping?
Reply With Quote #6

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.
Akuba is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 02-09-2016 , 12:11   Re: different client looping?
Reply With Quote #7

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.
Mitchell is offline
nergal
Veteran Member
Join Date: Apr 2012
Old 02-09-2016 , 15:14   Re: different client looping?
Reply With Quote #8

Quote:
Originally Posted by Mitchell View Post
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?
__________________

Last edited by nergal; 02-09-2016 at 15:14.
nergal is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 02-09-2016 , 15:51   Re: different client looping?
Reply With Quote #9

Quote:
Originally Posted by nergal View Post
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.

Last edited by klippy; 02-09-2016 at 15:54.
klippy is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 02-09-2016 , 15:54   Re: different client looping?
Reply With Quote #10

Quote:
Originally Posted by Kailo View Post
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.
Powerlord is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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