Raised This Month: $ Target: $400
 0% 

Optimizing code.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 04-10-2009 , 04:11   Optimizing code.
Reply With Quote #1

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;
        } 
__________________
xPaw is offline
Spunky
Senior Member
Join Date: May 2008
Location: Orlando, Fl.
Old 04-10-2009 , 04:26   Re: Optimizing code.
Reply With Quote #2

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.
Spunky is offline
Send a message via AIM to Spunky
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 04-10-2009 , 04:28   Re: Optimizing code.
Reply With Quote #3

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

Community / No support through PM
danielkza is offline
Spunky
Senior Member
Join Date: May 2008
Location: Orlando, Fl.
Old 04-10-2009 , 05:32   Re: Optimizing code.
Reply With Quote #4

Hmm. Ok then. O_o
Spunky is offline
Send a message via AIM to Spunky
hleV
Veteran Member
Join Date: Mar 2007
Location: Lithuania
Old 04-10-2009 , 06:13   Re: Optimizing code.
Reply With Quote #5

I think 2nd "if" can be "else if", same with 4th.
__________________
hleV is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 04-10-2009 , 08:35   Re: Optimizing code.
Reply With Quote #6

Quote:
Originally Posted by hleV View Post
I think 2nd "if" can be "else if", same with 4th.
You would use "else if" if the cases were mutually exclusive.
__________________
fysiks is offline
stupok
Veteran Member
Join Date: Feb 2006
Old 04-10-2009 , 13:46   Re: Optimizing code.
Reply With Quote #7

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++;
                }
            }
        }
    } 
__________________

Last edited by stupok; 04-10-2009 at 13:57. Reason: EDIT: oops, hold on
stupok is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 04-10-2009 , 13:52   Re: Optimizing code.
Reply With Quote #8

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 is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 04-10-2009 , 14:33   Re: Optimizing code.
Reply With Quote #9

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

Last edited by joaquimandrade; 04-10-2009 at 14:43.
joaquimandrade is offline
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 04-10-2009 , 16:11   Re: Optimizing code.
Reply With Quote #10

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;
        }
    }
}
__________________

Community / No support through PM
danielkza is offline
Reply


Thread Tools
Display Modes

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 02:19.


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