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

efficiency, if for escaping function question


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 02-12-2009 , 02:22   efficiency, if for escaping function question
Reply With Quote #1

Currently I have this:

Code:
public ham_player_killed( victim, killer, gib)
{
 if(!is_user_bot(killer) || victim == killer || get_user_team(killer) != get_user_team(victim))
  return HAM_IGNORED
...
Does the if statement check all three conditions every time even if the first and/or second returns true?

If not, I was thinking I should put the most common situation first and least for last. The idea being to do the least amount of work possible.

If so, would it be more beneficial to make three if statements in sequence with the most common occuring situation first?
fysiks is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 02-12-2009 , 02:39   Re: efficiency, if for escaping function question
Reply With Quote #2

if the first is wrong, it will check the second, otherwise it will return ham_ignored.
if the second is wrong it will check the third, otherwise it will return ham_ignored.
if the third is wrong you have passed all the check and you will continue, otherwise it will return ham_ignored.

So yeah, conditions are read in sequence.
Arkshine is offline
Brad
AMX Mod X Team Member
Join Date: Jun 2004
Old 02-12-2009 , 07:50   Re: efficiency, if for escaping function question
Reply With Quote #3

Quote:
Originally Posted by fysiks View Post
would it be more beneficial to make three if statements in sequence with the most common occuring situation first?
Yes.
__________________
Brad is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 02-12-2009 , 15:11   Re: efficiency, if for escaping function question
Reply With Quote #4

It seems to me that these two responses are contradicting.

I'll give both code situations explicitly if my wording was misinterpreted:

1 (rearranged from the first post):
Code:
 if(get_user_team(killer) != get_user_team(victim) || !is_user_bot(killer) || victim == killer)
  return HAM_IGNORED
2:
Code:
if(get_user_team(killer) != get_user_team(victim))
 return HAM_IGNORED
if(!is_user_bot(killer))
 return HAM_IGNORED
if(victim == killer)
 return HAM_IGNORED
Bottom Line: Which is fastest (to return HAM_IGNORED) when the kill is not a teamkill?

In code 2 if it's not a teamkill it will return HAM_INGNORED before it has a chance to evaluate !is_user_bot(killer) and victim == killer.

I posted because I assumed code 1 would evaluate all three conditions before returning HAM_IGNORED meaning code 2 would be faster in situations where it was not a team kill and is not a bot. I think this is what Brad said yes to.
fysiks is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 02-12-2009 , 15:38   Re: efficiency, if for escaping function question
Reply With Quote #5

Code:
if(get_user_team(killer) != get_user_team(victim))
 return HAM_IGNORED
if(!is_user_bot(killer))
 return HAM_IGNORED
if(victim == killer)
 return HAM_IGNORED
I would use this order:
Check if the killer is the victim.
Check if the killer is not a bot.
Check if the killer and the victim are on different teams.

Code:
if(victim == killer || !is_user_bot(killer) || get_user_team(killer) != get_user_team(victim))
    return HAM_IGNORED;
Reasoning:
1st check doesn't use any natives, only variable checks.
2nd check only uses 1 native.
3rd check uses 2 natives.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 02-12-2009 , 15:48   Re: efficiency, if for escaping function question
Reply With Quote #6

Ok, I understand your reason for order but my question is: should I use if if if or just the single if?

You posted code in both forms (one with your order and one without) which has confused me .

If I assumed something I would assume the if if if with your order would be most efficient.
fysiks is offline
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 02-12-2009 , 16:41   Re: efficiency, if for escaping function question
Reply With Quote #7

Quote:
Originally Posted by fysiks View Post
Ok, I understand your reason for order but my question is: should I use if if if or just the single if?

You posted code in both forms (one with your order and one without) which has confused me .

If I assumed something I would assume the if if if with your order would be most efficient.
Since the Pawn compiler is dumb (does no optimizations) using a single if() statement with multiple conditions should generate less code than separated statements. That'd not be the case in any 'good' compiler.
__________________

Community / No support through PM
danielkza 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 11:50.


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