Raised This Month: $ Target: $400
 0% 

[Optimize] Loop


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
^SmileY
Veteran Member
Join Date: Jan 2010
Location: Brazil [<o>]
Old 09-22-2011 , 22:40   [Optimize] Loop
Reply With Quote #1

Whats better usage and performance?

PHP Code:
    new iPlayers[32],iNum;
    
get_players(iPlayers,iNum,"ch");
    
    for(new 
i;iNum;i++)
    {
        if((
cs_get_user_team(iPlayers[i]) == CS_TEAM_SPECTATOR) || (cs_get_user_team(iPlayers[i]) == CS_TEAM_UNASSIGNED)) continue;
        
        
MainMenu(iPlayers[i]);
    } 
PHP Code:
    new iPlayers[32],iNum;
    
get_players(iPlayers,iNum,"ch");
    
    for(new 
i;iNum;i++)
    {
        switch(
cs_get_user_team(iPlayers[i]))
        {
            case 
CS_TEAM_SPECTATOR:
            {
                
/*Remove Players From Loop*/
                
continue;
            }
            case 
CS_TEAM_UNASSIGNED:
            {
                
/*Remove Players From Loop*/
                
continue;
            }

        }
        
MainMenu(iPlayers[i]);
    } 
PHP Code:
    new iPlayers[32],iNum;
    
get_players(iPlayers,iNum,"ch");
    
    for(new 
i;iNum;i++)
    {
        switch(
cs_get_user_team(iPlayers[i]))
        {
            case 
CS_TEAM_SPECTATOR CS_TEAM_UNASSIGNED:
            {
                
/*Remove Players From Loop*/
                
continue;
            }

        }
        
MainMenu(iPlayers[i]);
    } 
Thanks for read
__________________
Projects:

- See my Git Hub: https://github.com/SmileYzn
PHP Code:
set_pcvar_num(pCvar, !get_pcvar_num(pCvar)); 
^SmileY is offline
Send a message via MSN to ^SmileY Send a message via Skype™ to ^SmileY
Old 09-22-2011, 22:53
fmcTheKing
This message has been deleted by fmcTheKing.
e12harry
Member
Join Date: Apr 2010
Old 09-23-2011 , 03:07   Re: [Optimize] Loop
Reply With Quote #2

I think 3rd usage would be the best, but I would change one thing:
PHP Code:
    new iPlayers[32],iNum;
    
get_players(iPlayers,iNum,"ch");
    new 
player;
    for(new 
i;iNum;i++)
    {
        
player iPlayers[i];
        switch(
cs_get_user_team(player))
        {
            case 
CS_TEAM_SPECTATOR CS_TEAM_UNASSIGNED:
            {
                
/*Remove Players From Loop*/
                
continue;
            }

        }
        
MainMenu(player);
    } 
e12harry is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 09-23-2011 , 07:55   Re: [Optimize] Loop
Reply With Quote #3

Instead of trying to avoid unassigned/spectator, just look for what you want which is players on a team. A switch is really only good if you want to react a number of different ways based on a value. Here an if-statement is better since you only want to do something if player is on a team which is easy since T=1 and CT=2.

PHP Code:
new iPlayers32 ] , iNum iPlayer;
get_playersiPlayers iNum "ch" );

for ( new 
iNum i++ )
{
    
iPlayer iPlayers];

    if ( 
CS_TEAM_T <= cs_get_user_teamiPlayer ) <= CS_TEAM_CT )
        
MainMenuiPlayer );

__________________
Bugsy is offline
^SmileY
Veteran Member
Join Date: Jan 2010
Location: Brazil [<o>]
Old 09-23-2011 , 11:00   Re: [Optimize] Loop
Reply With Quote #4

Quote:
Originally Posted by Bugsy View Post
Instead of trying to avoid unassigned/spectator, just look for what you want which is players on a team. A switch is really only good if you want to react a number of different ways based on a value. Here an if-statement is better since you only want to do something if player is on a team which is easy since T=1 and CT=2.

PHP Code:
new iPlayers32 ] , iNum iPlayer;
get_playersiPlayers iNum "ch" );

for ( new 
iNum i++ )
{
    
iPlayer iPlayers];

    if ( 
CS_TEAM_T <= cs_get_user_teamiPlayer ) <= CS_TEAM_CT )
        
MainMenuiPlayer );

OK, i will be try now.
__________________
Projects:

- See my Git Hub: https://github.com/SmileYzn
PHP Code:
set_pcvar_num(pCvar, !get_pcvar_num(pCvar)); 
^SmileY is offline
Send a message via MSN to ^SmileY Send a message via Skype™ to ^SmileY
e12harry
Member
Join Date: Apr 2010
Old 09-26-2011 , 08:18   Re: [Optimize] Loop
Reply With Quote #5

Acording to: http://wiki.alliedmods.net/Optimizin...X_Scripting%29



Quote:
new team = get_user_team(player)
if (team == TEAM_T)
{
//...code
} else if (team == TEAM_CT) {
//...code
} else if (team == TEAM_SPECTATOR) {
//...code
}

Now, the compiler will only generate this:

CALL get_user_team
COMPARE+BRANCH
COMPARE+BRANCH
COMPARE+BRANCH

If get_user_team were an expensive operation (it's relatively cheap), we would have recalculated the entire result each branch of the if case.
Switch instead of If

If you can, you should use switch cases instead of if. This is because for an if statement, the compiler must branch to each consecutive if case. Using the example from above, observe the switch version:

new team = get_user_team(player)
switch (team)
{
case TEAM_T:
//code...
case TEAM_CT:
//code...
case TEAM_SPECTATOR:
//code...
}

This will generate what's called a "case table". Rather than worm through displaced if tests, the compiler generates a table of possible values, which the virtual machine knows to browse through:

CALL get_user_team
COMPARE
COMPARE
COMPARE
PHP Code:
 if ( CS_TEAM_T <= cs_get_user_teamiPlayer ) <= CS_TEAM_CT 
will take more operations than switch.
You can use:
PHP Code:
switch(cs_get_user_team(player))
        {
            case 
CS_TEAM_SPECTATOR CS_TEAM_UNASSIGNED:
            {
                
/*Remove Players From Loop*/
                
continue;
            }

        }
        
MainMenu(player); 
or
PHP Code:
switch(cs_get_user_team(player))
        {
            case 
CS_TEAM_T CS_TEAM_CT:
            {
               
MainMenu(player);
            }
        } 

As you can see in this case it is not worth arguing, but still I think switch is more efficient.

Last edited by e12harry; 09-26-2011 at 08:38.
e12harry is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 09-26-2011 , 08:22   Re: [Optimize] Loop
Reply With Quote #6

Quote the text please, I want to read where you obtained this information.

Even if it turns out to be a smidgen more efficient I would still use my method. You need to draw the line somewhere between being a hair more efficient and readability/less code.
__________________

Last edited by Bugsy; 09-26-2011 at 08:33.
Bugsy 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 19:35.


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