AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Which method is more efficient ? Task vs Each time check (https://forums.alliedmods.net/showthread.php?t=283095)

Hamartia 05-25-2016 03:38

Which method is more efficient ? Task vs Each time check
 
I want to understand how costly using set_task is ? Hence wanted to know the difference.

Why I ask this question is because this hook is going to be called numerous times on using grenade and wanted to know whether the calculation is worth of prevent a set_task.

This uses task to enable grenade.

Code:

public EventRoundStart()
{
    g_canNade = false;
    remove_task(TASK_ENABLE_NADE);
    set_task(get_pcvar_float(g_CvarTime),"EnableGrenade", TASK_ENABLE_NADE,"",0);
    arrayset(g_showBlockMsg, true, 32); 
}

public EnableGrenade(){
        g_canNade = true;
}

public OnCHEGrenade_PrimaryAttack( pEntity )
{
    if(get_pcvar_num(g_CvarEnable)){
           
        new id = get_pdata_cbase( pEntity , m_pPlayer , XO_CBASEPLAYERITEM );
       
        // Prevent nade in round start
        if(!g_canNade){
                if(g_showBlockMsg[id]){
                        client_print(id, print_center, "** You cant nade for first %d seconds **", floatround(get_pcvar_float(g_CvarTime)));
                        g_showBlockMsg[id] = false;
                }
                return HAM_SUPERCEDE;
        }

}

This checks each time when someone tries to use a grenade

Code:

public EventRoundStart()
{
    g_canNade = false;
    g_roundStartTime = get_gametime();
    arrayset(g_showBlockMsg, true, 32);
}

public OnCHEGrenade_PrimaryAttack( pEntity )
{
    if(get_pcvar_num(g_CvarEnable)){
           
        new id = get_pdata_cbase( pEntity , m_pPlayer , XO_CBASEPLAYERITEM );
        new Float:gameTime = get_gametime();
       
        // Prevent nade in round start
        if(!g_canNade){               
                if(gameTime - g_roundStartTime > get_pcvar_float(g_CvarTime)){
                        g_canNade = true;
                }else{
               
                        if(g_showBlockMsg[id]){
                                client_print(id, print_center, "** You cant nade for first %d seconds **", floatround(get_pcvar_float(g_CvarTime)));
                                g_showBlockMsg[id] = false;
                        }
                        return HAM_SUPERCEDE;
                }
        }

}


Black Rose 05-25-2016 05:34

Re: Which method is more efficient ? Task vs Each time check
 
I would say the second option but it really makes no difference.
But save the time in a global var only. You don't need the bool. Look at antiflood.

fysiks 05-25-2016 09:11

Re: Which method is more efficient ? Task vs Each time check
 
Typically, if the function that you are trying to perform naturally works in a hook, you should prefer that method.

It really depends on what you are trying to do. In this case, I would probably use the set_task() because it's easier to understand what the code is doing and as far as efficiency goes it's going to be negligibly different.

Hamartia 05-25-2016 09:45

Re: Which method is more efficient ? Task vs Each time check
 
Thank you for checking it and giving your opinion.

@Black Rose I used the bool because I thought why to use a comparison check since the method above will be called numerous times when the player selects grenade and presses fire button (because it wont allow you to throw and the hook is gonna be called again). After the x seconds, that condition will be true for that round, so used a boolean.

@fysiks Yes exactly! It totally depends up on what am trying to do. Sorry that I didn't explain that. Why I asked this question is because this hook is going to be called numerous times on using grenade and wanted to know whether the calculation is worth of prevent a set_task. I ll edit the question.

I do understand that I might be making the usual mistake: Over optimization. Hence wanted your views.


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

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