AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [Optimize] Loop (https://forums.alliedmods.net/showthread.php?t=167997)

^SmileY 09-22-2011 22:40

[Optimize] Loop
 
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 :x

e12harry 09-23-2011 03:07

Re: [Optimize] Loop
 
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);
    } 


Bugsy 09-23-2011 07:55

Re: [Optimize] Loop
 
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 );



^SmileY 09-23-2011 11:00

Re: [Optimize] Loop
 
Quote:

Originally Posted by Bugsy (Post 1561362)
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.

e12harry 09-26-2011 08:18

Re: [Optimize] Loop
 
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.

Bugsy 09-26-2011 08:22

Re: [Optimize] Loop
 
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.


All times are GMT -4. The time now is 19:35.

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