SA-MP Forums Archive
What is faster: if or switch? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: What is faster: if or switch? (/showthread.php?tid=598299)



What is faster: if or switch? - BiosMarcel - 10.01.2016

I'd really like to know if any of these two is faster than the other one.
Personally i think no cause it basically is the same, but i'd still like to know.
Hope someone can tell me, thanks.

PHP код:
switch(listitem)
{
    case 
0: .....
    case 
1: .....
    default: .....

or

PHP код:
if(listitem == 0)
{
....
}
else if(
listitem == 1)
{
....
}
else
{
....

greetings Marcel


Re: What is faster: if or switch? - Darkwood17 - 10.01.2016

Switch is faster, because it's more efficient and goes directly to the correct case.
https://sampwiki.blast.hk/wiki/Keywords:Statements#switch


Re: What is faster: if or switch? - BiosMarcel - 10.01.2016

thanks, i should have ******d myself


Re: What is faster: if or switch? - Runn3R - 10.01.2016

Switch is only faster if there is more questions than one or two but if there is less than that if is faster


Re: What is faster: if or switch? - Vince - 10.01.2016

I seem to remember that the compiled code looks pretty much the same, but obviously a switch looks neater and is easier to read. Personally I am also not a fan of "else if". To me, that attests bad coding style.