Raised This Month: $32 Target: $400
 8% 

[SOLVED] IF Performance (Yeah, IF)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
^SmileY
Veteran Member
Join Date: Jan 2010
Location: Brazil [<o>]
Old 06-14-2018 , 11:56   [SOLVED] IF Performance (Yeah, IF)
Reply With Quote #1

Code:
public FMVoiceSetClientListening(Recv,Sender,bool:Listen)
{
	if(Recv != Sender)
	{
		if(is_user_connected(Recv) && is_user_connected(Sender))
		{
			if(get_user_team(Recv) == get_user_team(Sender))
			{
				engfunc(EngFunc_SetClientListening,Recv,Sender,true);
				return FMRES_SUPERCEDE;
			}
		}
	}
	
	return FMRES_IGNORED;
}
Versus

Code:
public FMVoiceSetClientListening(Recv,Sender,bool:Listen)
{
	if(Recv != Sender && is_user_connected(Recv) && is_user_connected(Sender) && get_user_team(Recv) == get_user_team(Sender))
	{
		engfunc(EngFunc_SetClientListening,Recv,Sender,true);
		return FMRES_SUPERCEDE;
	}
	
	return FMRES_IGNORED;
}
I made this code to ask, if first is better than second example.
Because if first check is false, the plugin will not call other (sub) checks

At second example same if is calling both functions at same time.
Someone more skilled than me can sane this doubt?

Thanks again
__________________
Projects:

- See my Git Hub: https://github.com/SmileYzn
PHP Code:
set_pcvar_num(pCvar, !get_pcvar_num(pCvar)); 
^SmileY is offline
Send a message via MSN to ^SmileY Send a message via Skype™ to ^SmileY
NiHiLaNTh
Way Past Expiration
Join Date: May 2009
Location: Latvia
Old 06-14-2018 , 12:06   Re: IF Performance (Yeah, IF)
Reply With Quote #2

If you have multiple AND's chained, than if the first expression is false, the compiler won't check the others.
__________________

NiHiLaNTh is offline
Send a message via Skype™ to NiHiLaNTh
maqi
Senior Member
Join Date: Apr 2017
Location: Serbia
Old 06-14-2018 , 12:14   Re: IF Performance (Yeah, IF)
Reply With Quote #3

Exactly, as far as i know, if you are chaining AND's, it goes left to right, as soon as one is false, it won't check further. For OR's it won't check the others as soon as one is true.

Following that logic the thing you should look out for, is how likely an expression is to be true, place the less likely ones up front But anyway i think that the performance gain from this is minimal, but good practice.

Last edited by maqi; 06-14-2018 at 12:14.
maqi is offline
^SmileY
Veteran Member
Join Date: Jan 2010
Location: Brazil [<o>]
Old 06-14-2018 , 12:27   Re: IF Performance (Yeah, IF)
Reply With Quote #4

Quote:
Originally Posted by NiHiLaNTh View Post
If you have multiple AND's chained, than if the first expression is false, the compiler won't check the others.
Is execution time not at compile time no?

@maqi
I doubt that in execution time the plugin will not continue checking before first check fail since it in same line.
Also i guess performance is relative that what is calling the function (How many times) / frame or second for example.

Lets see other users to have sure
__________________
Projects:

- See my Git Hub: https://github.com/SmileYzn
PHP Code:
set_pcvar_num(pCvar, !get_pcvar_num(pCvar)); 
^SmileY is offline
Send a message via MSN to ^SmileY Send a message via Skype™ to ^SmileY
maqi
Senior Member
Join Date: Apr 2017
Location: Serbia
Old 06-14-2018 , 12:30   Re: IF Performance (Yeah, IF)
Reply With Quote #5

That is exactly what im saying, i could be wrong, but i was always thought like this, I just wanted to pass it on.
maqi is offline
^SmileY
Veteran Member
Join Date: Jan 2010
Location: Brazil [<o>]
Old 06-14-2018 , 12:46   Re: IF Performance (Yeah, IF)
Reply With Quote #6

I tried to check pawn documentation, but this not explain nothing

I will use FIRST example to have sure
__________________
Projects:

- See my Git Hub: https://github.com/SmileYzn
PHP Code:
set_pcvar_num(pCvar, !get_pcvar_num(pCvar)); 

Last edited by ^SmileY; 06-14-2018 at 14:52. Reason: FIRST
^SmileY is offline
Send a message via MSN to ^SmileY Send a message via Skype™ to ^SmileY
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 06-14-2018 , 14:06   Re: IF Performance (Yeah, IF)
Reply With Quote #7

They will probably compile to same bytecode (or with little differences that won't matter to performance). What Maqi said is true and that phenomenon is called "short-circuit evaluation".
__________________

Last edited by klippy; 06-14-2018 at 14:07.
klippy is offline
^SmileY
Veteran Member
Join Date: Jan 2010
Location: Brazil [<o>]
Old 06-14-2018 , 14:57   Re: IF Performance (Yeah, IF)
Reply With Quote #8

Quote:
Originally Posted by KliPPy View Post
They will probably compile to same bytecode (or with little differences that won't matter to performance). What Maqi said is true and that phenomenon is called "short-circuit evaluation".
Anyway first example is more readable than first, short-circuit evaluation is not mentioned in pawn anyway thanks
__________________
Projects:

- See my Git Hub: https://github.com/SmileYzn
PHP Code:
set_pcvar_num(pCvar, !get_pcvar_num(pCvar)); 
^SmileY is offline
Send a message via MSN to ^SmileY Send a message via Skype™ to ^SmileY
naz7
Junior Member
Join Date: Jun 2018
Location: Spain
Old 06-14-2018 , 19:33   Re: IF Performance (Yeah, IF)
Reply With Quote #9

Quote:
Originally Posted by ^SmileY View Post
Anyway first example is more readable than first, short-circuit evaluation is not mentioned in pawn anyway thanks
In my opinion the first is more readable as long as it doesn't make the function too big. I like functions that can fit in the screen without having to scroll, either horizontally or vertically. Generally I would go with the second option, which as KliPPy stated probably compiles to the same bytecode. If the condition gets too wide you can break the line, and have 2 conditional parts in a line and other 2 below. Anyways I think the most important thing is that your code stays consistent, if you go with that code style keep it along the whole script.
naz7 is offline
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 06-14-2018 , 21:19   Re: [SOLVED] IF Performance (Yeah, IF)
Reply With Quote #10

I like more with return values, but that's doesn't matter if you do not do wrong.

Code:
public FMVoiceSetClientListening(Recv,Sender,bool:Listen) {     if (Recv == Sender)         return FMRES_IGNORED;     if (!is_user_connected(Recv) || !is_user_connected(Sender))         return FMRES_IGNORED;     if (get_user_team(Recv) != get_user_team(Sender))         return FMRES_IGNORED;         engfunc(EngFunc_SetClientListening,Recv,Sender,true);     return FMRES_SUPERCEDE; }
__________________








CrazY. 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 15:24.


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