What is faster: if or switch?
#1

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
Reply
#2

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

thanks, i should have ******d myself
Reply
#4

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

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


Forum Jump:


Users browsing this thread: 1 Guest(s)