AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Optimizing code. (https://forums.alliedmods.net/showthread.php?t=89745)

xPaw 04-10-2009 04:11

Optimizing code.
 
Hai, how i can optimize this part of code?
PHP Code:

        if( equalszClassname"func_button" ) ) {
            new 
szTarget[32], i;
            
peviEntitypev_targetszTarget31 );
            
            if( !
gButtons[START] )
                for( 
0sizeof szCounterStarti++ )
                    if( 
equalszTargetszCounterStart[i] ) )
                        
gButtons[START] = iEntity;
            
            if( !
gButtons[START2] && gButtons[START] )
                for( 
0sizeof szCounterStarti++ )
                    if( 
iEntity != gButtons[START] && equalszTargetszCounterStart[i] ) )
                        
gButtons[START2] = iEntity;
            
            if( !
gButtons[STOP] )
                for( 
0sizeof szCounterStopi++ )
                    if( 
equalszTargetszCounterStop[i] ) )
                        
gButtons[STOP] = iEntity;
            
            if( !
gButtons[STOP2] && gButtons[STOP] )
                for( 
0sizeof szCounterStopi++ )
                    if( 
iEntity != gButtons[STOP] && equalszTargetszCounterStop[i] ) )
                        
gButtons[STOP2] = iEntity;
        } 


Spunky 04-10-2009 04:26

Re: Optimizing code.
 
Only thing I can see wrong is calling sizeof when you probably already know the size. Waste of resources. Can't tell you more without understanding what you're trying to do, which I don't.

danielkza 04-10-2009 04:28

Re: Optimizing code.
 
Quote:

Originally Posted by Spunky (Post 802002)
Only thing I can see wrong is calling sizeof when you probably already know the size. Waste of resources. Can't tell you more without understanding what you're trying to do, which I don't.

sizeof() is a compiler directive, it will be preprocessed to the actual value, it is no different at all than just using a number, the final result passed to the compiler will be the same.

Spunky 04-10-2009 05:32

Re: Optimizing code.
 
Hmm. Ok then. O_o

hleV 04-10-2009 06:13

Re: Optimizing code.
 
I think 2nd "if" can be "else if", same with 4th.

fysiks 04-10-2009 08:35

Re: Optimizing code.
 
Quote:

Originally Posted by hleV (Post 802063)
I think 2nd "if" can be "else if", same with 4th.

You would use "else if" if the cases were mutually exclusive.

stupok 04-10-2009 13:46

Re: Optimizing code.
 
I suspect that this could be optimized more, if I could see more of your code. I'm not sure.


Anyways, I'm not sure if this is compatible with your code. I think it will probably work:

PHP Code:

    if( equalszClassname"func_button" ) )
    {
        static 
szTarget[32];
        
peviEntitypev_targetszTarget31 );
        
        new 
iNumAssigned 0
        
        
// only if sizeof szCounterStart == sizeof szCounterStop
        
for( new 0sizeof szCounterStarti++ )
        {
            if( 
iNumAssigned == )
                break;
            
            if( 
equalszTargetszCounterStart[i] ) )
            {
                if( !
gButtons[START] )
                {
                    
gButtons[START] = iEntity;
                    
iNumAssigned++;
                }
                else
                {
                    
gButtons[START2] = iEntity;
                    
iNumAssigned++;
                }
            }
            else if( 
equalszTargetszCounterStop[i] ) )
            {
                if( !
gButtons[STOP] )
                {
                    
gButtons[STOP] = iEntity;
                    
iNumAssigned++;
                }
                else
                {
                    
gButtons[STOP2] = iEntity;
                    
iNumAssigned++;
                }
            }
        }
    } 


joaquimandrade 04-10-2009 13:52

Re: Optimizing code.
 
What you want this to do:
PHP Code:

    if( !gButtons[START] ) 
                for( 
0sizeof szCounterStarti++ ) 
                    if( 
equalszTargetszCounterStart[i] ) ) 
                        
gButtons[START] = iEntity

?

Find the first (equal( szTarget, szCounterStart[i]) or the last one? As you have it its finding the last one. If it can be the first, add a break. If you want the last one change the for to
PHP Code:

for( sizeof szCounterStart ->= i-- ) 

and add a break.

If you explain it more i can optimize it.

joaquimandrade 04-10-2009 14:33

Re: Optimizing code.
 
If i understood it right:

PHP Code:

new startIndex = -1
new endIndex

if(!gButtons[START])
{
    
startIndex START
    endIndex 
START + !gButtons[START2]
}
else if(!
gButtons[START2])
    
startIndex endIndex START2;

if(
startIndex != -1)
{
    for( 
0sizeof szCounterStarti++ )
    {
        if( 
equalszTargetszCounterStart[i] ) )
        {
            
gButtons[startIndex++] = iEntity;        
            
            if(
startIndex endIndex)
                break;
        }
    }


Then the same for stop. (I'm assuming that (START +1) = (START2))

Edit: i think that i understood it wrong or there is something wrong with the provided code. I will leave the code here anyway. I will try to optimize when xPaw explain he's code.

I think that this:

PHP Code:

gButtons[START] = iEntity

Should have some relation with i not with iEntity.

danielkza 04-10-2009 16:11

Re: Optimizing code.
 
From what I understood, it's a very simple snippet, what it does is check if an entity is a 'func_button'. If positive, loop through two string sets, checking if the target name matches. If it does, store the entity index on gButtons[START], if it's the first match, or gButtons[START2] if one was already found. Do the same with the stop buttons.

My attempt:
Code:

new iStartCount = 0, iStopCount = 0

if(equal( szClassname, "func_button"))
{
    new szTarget[32], i;
    pev(iEntity, pev_target, szTarget, charsmax(szTarget));
   
    if(!iStartCount || (iStartCount == 1 && iEntity != gButtons[START]))
    {
        for( i = 0; i < sizeof(szCounterStart); i++)
        {
            if(equal(szTarget, szCounterStart[i]))
                gButtons[!(iStartCount++) ? START : START2] = iEntity;
        }
    }
    else if(!iStopCount || (iStopCount == 1 && iEntity != gButtons[STOP]))
    {
        for( i = 0; i < sizeof(szCounterStop); i++)
        {
            if(equal(szTarget, szCounterStop[i]))
                gButtons[!(iStopCount++) ? STOP : STOP2] = iEntity;
        }
    }
}



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

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