Switch VS IF
#1

I don't know what to use, which one is faster,
Example:

First code
pawn Код:
new rand = random(100);

if(rand == 0)   print("cat");
else if(rand == 1) print("dog");
else if(rand == 2) print("elephant"); // lol
...
Second code
pawn Код:
switch(random(100))
{
    case 0: print("cat");
    case 1: print("dog");
    case 2: print("elephant");
}
Reply
#2

Use switch, it's faster than else/if.
Reply
#3

Quote:
Originally Posted by Don Correlli
Use switch, it's faster than else/if.
How can you know that?
Reply
#4

Quote:
Originally Posted by Luka™
Quote:
Originally Posted by Don Correlli
Use switch, it's faster than else/if.
How can you know that?
Well think about it, instead of checking EVERY case, the switch goes directly to the correct one.
Reply
#5

It just is. Tests have been done n such. Here, I just ran a test of both of them.

Here's the script to test them.
http://pastebin.com/m568584f1

Here's the results.
Код:
[14:17:29] dog
[14:17:29] dog
[14:17:29] elephant
[14:17:29] elephant
[14:17:29] dog
[14:17:29] dog
[14:17:29] car
[14:17:29] dog
[14:17:29] elephant
[14:17:29] elephant
[14:17:29] dog
[14:17:29] elephant
[14:17:29] dog
[14:17:29] car
[14:17:29] dog
[14:17:29] dog
[14:17:29] Time 1: 0307, time 2: 0055
Obviously I cut off part of the dog, car, elephant part because it was really long.
Reply
#6

Quote:
Originally Posted by Luka™
Quote:
Originally Posted by Don Correlli
Use switch, it's faster than else/if.
How can you know that?
It has been clocked before

EDIT: See post above mine
Reply
#7

I should imagine switch would be faster for that purpose as that is what it is built for.
Reply
#8

Well that test was sort of inaccurate because the random number could not have been 1, 2, or 3 meaning it wouldn't have had to call any of the prints. Making it faster. But still, it's faster.
Reply
#9

Quote:
Originally Posted by Don Correlli
Use switch, it's faster than else/if.
That is a false statement as it depend of what you do in the switch, maybe doing it with if's will be easier, require less operations and so be faster.

Try to do a switch with a float value and cases such as 'case 0.0 .. 10.0', and then compare the speed with a similar if structure, you will see that the if structure is much faster.
Reply
#10

@0rb: i was referring to his code.
Reply
#11

Quote:
Originally Posted by Don Correlli
@0rb: i was referring to his code.
I just gave example, I ask which is faster in general sense.
Reply
#12

In most cases switch is faster. Search on ******, there are also many useful answers.
Reply
#13

Actually "switch" doesn't jump right into right case because it CAN NOT !
Speed is gained by generating only ONE(1) result and then comparing them to CASES, and "if" generate ONE(1) result per EVERY IF and then compare EACH so it's PURE LOGIC that switch is faster in that way.
Reply
#14

Quote:
Originally Posted by Y_Leѕѕ
Don't use ****** - PAWN's switches are different to C's switches
I know, i wasn't looking for C's switches.

https://sampwiki.blast.hk/wiki/Keywords:Statements#switch
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)