Raised This Month: $51 Target: $400
 12% 

Which method is more efficient ? Task vs Each time check


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Hamartia
Member
Join Date: Oct 2015
Old 05-25-2016 , 03:38   Which method is more efficient ? Task vs Each time check
Reply With Quote #1

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

Last edited by Hamartia; 05-25-2016 at 09:45.
Hamartia is offline
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 05-25-2016 , 05:34   Re: Which method is more efficient ? Task vs Each time check
Reply With Quote #2

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.
__________________
Black Rose is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 05-25-2016 , 09:11   Re: Which method is more efficient ? Task vs Each time check
Reply With Quote #3

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

Last edited by fysiks; 05-25-2016 at 09:12.
fysiks is online now
Hamartia
Member
Join Date: Oct 2015
Old 05-25-2016 , 09:45   Re: Which method is more efficient ? Task vs Each time check
Reply With Quote #4

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

Last edited by Hamartia; 05-25-2016 at 09:46.
Hamartia is offline
Old 05-25-2016, 10:56
HamletEagle
This message has been deleted by HamletEagle.
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:05.


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