Raised This Month: $ Target: $400
 0% 

Optimizing code.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 04-10-2009 , 14:33   Re: Optimizing code.
Reply With Quote #1

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 #2

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

Quote:
Originally Posted by danielkza View Post
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;
        }
    }
}
PHP Code:
      if(!iStartCount || (iStartCount == && iEntity != gButtons[START])) 
and

PHP Code:
     else if(!iStopCount || (iStopCount == && iEntity != gButtons[STOP])) 
Don't make sense because iStartCount and iStopCount will be 0.
__________________

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

Quote:
Originally Posted by joaquimandrade View Post
PHP Code:
      if(!iStartCount || (iStartCount == && iEntity != gButtons[START])) 
and

PHP Code:
     else if(!iStopCount || (iStopCount == && iEntity != gButtons[STOP])) 
Don't make sense because iStartCount and iStopCount will be 0.
In the first run they'll be 0, but they'll be incremented inside the loop if a match is found.

And i'm not sure if it should be an if or an elseif in the second part, xPaw should know and can tell us.
__________________

Community / No support through PM
danielkza is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 04-10-2009 , 17:10   Re: Optimizing code.
Reply With Quote #5

Quote:
Originally Posted by danielkza View Post
In the first run they'll be 0, but they'll be incremented inside the loop if a match is found.

And i'm not sure if it should be an if or an elseif in the second part, xPaw should know and can tell us.
They are not inside a loop.
__________________
joaquimandrade is offline
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 04-10-2009 , 17:15   Re: Optimizing code.
Reply With Quote #6

Quote:
Originally Posted by joaquimandrade View Post
They are not inside a loop.
You got me confused now.

If iStartCount is 0, the code will continue, loops through all szCounterStart strings. If a match is found, iStartCount is incremented. If it was 0, we store iEntity in gButtons[START], if it was 1, we store it in gButtons[START2]

If iStartCount is 1, we only proceed with the code if iEntity is not equal to the already stored iEntity in gButton[START]. If we find a match again, iStartCount is incremented to 2, and the code won't run anymore.
__________________

Community / No support through PM
danielkza is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 04-10-2009 , 17:16   Re: Optimizing code.
Reply With Quote #7

Quote:
Originally Posted by danielkza View Post
You got me confused now.

If iStartCount is 0, the code will continue, loops through all szCounterStart strings. If a match is found, iStartCount is incremented. If it was 0, we store iEntity in gButtons[START], if it was 1, we store it in gButtons[START2]

If iStartCount is 1, we only proceed with the code if iEntity is not equal to the already stored iEntity in gButton[START]. If we find a match again, iStartCount is incremented to 2, and the code won't run anymore.
Check your code again.

This:

PHP Code:
if(!iStartCount || (iStartCount == && iEntity != gButtons[START])) 
Is outside of the loop.
__________________
joaquimandrade 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 02:19.


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