Raised This Month: $ Target: $400
 0% 

If to switch optimising


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
DarkSidero
Junior Member
Join Date: Jul 2009
Location: Romania
Old 07-23-2009 , 16:37   If to switch optimising
Reply With Quote #1

Hello there! I'll make it short. We've got the following code:

Code:
public abilities(id)
{
    if(a_invisibility[id] && !a_stoneshield[id] && !a_rage[id])
    {
    set_task(0.1, "invisibilitydo")
    }

    if(!a_invisibility[id] && a_stoneshield[id] && !a_rage[id])
    {
    set_task(0.1, "stoneshielddo")
    }

    if(!a_invisibility[id] && !a_stoneshield[id] && a_rage[id])
    {
    set_task(0.1, "ragedo")
    }

    if(!a_invisibility[id] && !a_stoneshield[id] && !a_rage[id])
    {
    return PLUGIN_HANDLED
    }            
}
I'd want to use this code in a plugin, and that, from what I've read, would cause a lot of processor branching, thus making the plugin work harder and giving the processor a hard job. I'd like to know how is it possible to optimise it to do the same thing (from what I know up until now, it should be done through a "switch" command, but I guess there are other different ways either).
__________________
There are 10 types of people in the world: those who understand binary and those who do not.

Professions Mod

Last edited by DarkSidero; 07-29-2009 at 10:33.
DarkSidero is offline
Send a message via Yahoo to DarkSidero
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 07-23-2009 , 16:55   Re: If to switch optimising
Reply With Quote #2

Code:
public abilities(id)
{
    const iFlagInvisible = 1;
    const iFlagShield = 2;
    const iFlagRage = 4;
    
    new iFlags = (_:a_invisibility[id]) + (_:a_stoneshield[id] * 2) + (_:a_rage[id] * 4);
    
    switch( iFlags )
    {
        case iFlagInvisible:
        {
            set_task(0.1, "invisibilitydo")
        }
        case iFlagShield:
        {
            set_task(0.1, "stoneshielddo")
        }
        case iFlagRage:
        {
            set_task(0.1, "ragedo")
        }
        case 0:
        {
            return PLUGIN_HANDLED
        }
    }
}
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!

Last edited by Exolent[jNr]; 07-23-2009 at 19:20.
Exolent[jNr] is offline
DarkSidero
Junior Member
Join Date: Jul 2009
Location: Romania
Old 07-23-2009 , 16:58   Re: If to switch optimising
Reply With Quote #3

That was fast, thanks a lot exolent .
DarkSidero is offline
Send a message via Yahoo to DarkSidero
hleV
Veteran Member
Join Date: Mar 2007
Location: Lithuania
Old 07-23-2009 , 17:05   Re: If to switch optimising
Reply With Quote #4

I doubt switch() will work this way.
__________________
hleV is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 07-23-2009 , 17:29   Re: If to switch optimising
Reply With Quote #5

Quote:
Originally Posted by hleV View Post
I doubt switch() will work this way.
Did you take a look at my code?
It will definitely work.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 07-23-2009 , 17:36   Re: If to switch optimising
Reply With Quote #6

It will not work because iFlag* are not constants. Remove 'static' and it will work.
__________________
Arkshine is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 07-23-2009 , 19:21   Re: If to switch optimising
Reply With Quote #7

Quote:
Originally Posted by Arkshine View Post
It will not work because iFlag* are not constants. Remove 'static' and it will work.
Didn't think about that. Fixed.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
DarkSidero
Junior Member
Join Date: Jul 2009
Location: Romania
Old 07-24-2009 , 04:22   Re: If to switch optimising
Reply With Quote #8

Thanks a lot both .
DarkSidero is offline
Send a message via Yahoo to DarkSidero
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 07-24-2009 , 13:18   Re: If to switch optimising
Reply With Quote #9

Do you really want to use a switch() ?
Because you can do the same with less code:

Code:
public abilities( id )
{
    if( a_invisibility[ id ] )
    {
        set_task( 0.1, "invisibilitydo" );
    }
    else if( a_stoneshield[ id ] )
    {
        set_task( 0.1, "stoneshielddo" );
    }
    else if( a_rage[ id ] )
    {
        set_task( 0.1, "ragedo" );
    }
    else
    {
        return PLUGIN_HANDLED;
    }
}
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
DarkSidero
Junior Member
Join Date: Jul 2009
Location: Romania
Old 07-25-2009 , 15:42   Re: If to switch optimising
Reply With Quote #10

I prefer "if"s either, but they put the processor to do a harder job than switch() would, that's why I asked for a way to use switch in this case, I had no ideea it would be that difficult. The code you gave me doesn't work, compilation errors, but it doesn't matter, I've decided to use "if"s afterall . Thanks for the help.
DarkSidero is offline
Send a message via Yahoo to DarkSidero
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 18:23.


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