Raised This Month: $ Target: $400
 0% 

Question related to Swich Statements.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Drixevel
AlliedModders Donor
Join Date: Sep 2009
Location: Somewhere headbangin'
Old 11-26-2013 , 08:39   Question related to Swich Statements.
Reply With Quote #1

Not entirely convinced by the performance values so I figured I'd post here for a quick answer.

Would this for example...
Code:
new value;
if (value == 1)
{
	//Do stuff
}
else if (value == 2)
{
	//Do other stuff
}
Be a bit more effective in terms of performance or this...
Code:
new value;
switch (value)
{
	case 1:
		{
			//Do stuff
		}
	case 2:
		{
			//Do other stuff
		}
}
Purely, which one runs better or does it not matter? I like Switch statements a lot better.

Cheers,
- Jack

Last edited by Drixevel; 11-26-2013 at 08:39.
Drixevel is offline
bl4nk
SourceMod Developer
Join Date: Jul 2007
Old 11-26-2013 , 08:57   Re: Question related to Swich Statements.
Reply With Quote #2

It's really up to the compiler, and I'm not an expert on how that works with SourcePawn. Sometimes an if/else/elseif statement could be just as fast as when using a switch, but most other times the switch would be faster. In the specific case you posted, there probably wouldn't be a difference in which you used.

Last edited by bl4nk; 11-26-2013 at 08:57. Reason: typo
bl4nk is offline
Impact123
Veteran Member
Join Date: Oct 2011
Location: Germany
Old 11-26-2013 , 09:11   Re: Question related to Swich Statements.
Reply With Quote #3

Switch is said to be faster (AMX), but the difference is very very small, use what fits your style the best.
You should rather spent your time optimizing the code inside the statement you use.
Here are some tests, each function is called 500 times.

Results


Offtopic
__________________

Last edited by Impact123; 11-26-2013 at 09:29.
Impact123 is offline
friagram
Veteran Member
Join Date: Sep 2012
Location: Silicon Valley
Old 11-26-2013 , 10:56   Re: Question related to Swich Statements.
Reply With Quote #4

If you are doing finite comparisons, should probly use switch. It's more human-readable anyway.
__________________
Profile - Plugins
Add me on steam if you are seeking sp/map/model commissions.
friagram is offline
Drixevel
AlliedModders Donor
Join Date: Sep 2009
Location: Somewhere headbangin'
Old 11-26-2013 , 13:25   Re: Question related to Swich Statements.
Reply With Quote #5

Thank you for the tips guys. I'm trying to optimize my plugins to run a lot smoother than they do now. I like using switch statements because it's just overall easier to manage.
Drixevel is offline
MasterOfTheXP
Veteran Member
Join Date: Aug 2011
Location: Cloudbank
Old 11-26-2013 , 15:15   Re: Question related to Swich Statements.
Reply With Quote #6

Another situation in which to pretty much always use switch statements instead of if statements are lists like these, where the repetitiveness speaks for itself:
PHP Code:
if (== || == || == || == || == 5) return; 
PHP Code:
switch (i) { case 12345: return; } 
__________________
Plugins / My Steam / TF2 Sandbox (plugin beta testing!)

Last edited by MasterOfTheXP; 11-26-2013 at 15:15.
MasterOfTheXP is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 11-26-2013 , 15:21   Re: Question related to Swich Statements.
Reply With Quote #7

Generally, switch is more efficient than if/elseif/else because switch can be implemented using a jump table. I don't know if the SourcePawn compiler does this or not.
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
necavi
Veteran Member
Join Date: Sep 2010
Old 11-26-2013 , 15:46   Re: Question related to Swich Statements.
Reply With Quote #8

At least in C they can be implemented any number of ways by the compiler...a series of if/else type structures, a jump table, or probably one or two others that I'm forgetting, but I believe that pawn uses jump tables.

Last edited by necavi; 11-26-2013 at 15:46.
necavi is offline
sdz
Senior Member
Join Date: Feb 2012
Old 11-27-2013 , 04:13   Re: Question related to Swich Statements.
Reply With Quote #9

In my opinion and experiences, I prefer switch for a few reasons:

  • Looks alot better then else if else if else if
  • case 69:
  • Just alot easier to write

Last edited by sdz; 11-27-2013 at 04:14.
sdz 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 06:44.


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