AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   If to switch optimising (https://forums.alliedmods.net/showthread.php?t=98222)

DarkSidero 07-23-2009 16:37

If to switch optimising
 
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).

Exolent[jNr] 07-23-2009 16:55

Re: If to switch optimising
 
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
        }
    }
}


DarkSidero 07-23-2009 16:58

Re: If to switch optimising
 
That was fast, thanks a lot exolent :).

hleV 07-23-2009 17:05

Re: If to switch optimising
 
I doubt switch() will work this way.

Exolent[jNr] 07-23-2009 17:29

Re: If to switch optimising
 
Quote:

Originally Posted by hleV (Post 880017)
I doubt switch() will work this way.

Did you take a look at my code?
It will definitely work.

Arkshine 07-23-2009 17:36

Re: If to switch optimising
 
It will not work because iFlag* are not constants. Remove 'static' and it will work.

Exolent[jNr] 07-23-2009 19:21

Re: If to switch optimising
 
Quote:

Originally Posted by Arkshine (Post 880037)
It will not work because iFlag* are not constants. Remove 'static' and it will work.

Didn't think about that. Fixed.

DarkSidero 07-24-2009 04:22

Re: If to switch optimising
 
Thanks a lot both :).

Exolent[jNr] 07-24-2009 13:18

Re: If to switch optimising
 
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;
    }
}


DarkSidero 07-25-2009 15:42

Re: If to switch optimising
 
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.


All times are GMT -4. The time now is 18:23.

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