Switch VS IF - 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)
+---- Forum: Help Archive (
https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Switch VS IF (
/showthread.php?tid=109867)
Switch VS IF -
Luka P. - 22.11.2009
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");
}
Re: Switch VS IF -
Correlli - 22.11.2009
Use switch, it's faster than else/if.
Re: Switch VS IF -
Luka P. - 22.11.2009
Quote:
Originally Posted by Don Correlli
Use switch, it's faster than else/if.
|
How can you know that?
Re: Switch VS IF -
Kyosaur - 22.11.2009
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.
Re: Switch VS IF -
Backwardsman97 - 22.11.2009
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.
Re: Switch VS IF -
FUNExtreme - 22.11.2009
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
Re: Switch VS IF -
gotenks918 - 22.11.2009
I should imagine switch would be faster for that purpose as that is what it is built for.
Re: Switch VS IF -
Backwardsman97 - 22.11.2009
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.
Re: Switch VS IF -
yom - 22.11.2009
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.
Re: Switch VS IF -
Correlli - 22.11.2009
@0rb: i was referring to his code.
Re: Switch VS IF -
Luka P. - 22.11.2009
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.
Re: Switch VS IF -
Correlli - 22.11.2009
In most cases switch is faster. Search on ******, there are also many useful answers.
Re: Switch VS IF -
Google63 - 22.11.2009
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.
Re: Switch VS IF -
Correlli - 22.11.2009
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