SA-MP Forums Archive
Switch is faster than 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)
+--- Thread: Switch is faster than If? (/showthread.php?tid=636408)



Switch is faster than If? - rolex - 26.06.2017

Hi bro's!


Which of these two is faster ?

PHP код:
if(var == 1) { return X; } 
or

PHP код:
switch(var) { case 1: return X; } 
? and why?


And.. how can i do my own benchmark?


Thank you!


Re: Switch is faster than If? - Toroi - 26.06.2017

https://sampforum.blast.hk/showthread.php?tid=218491


Re: Switch is faster than If? - BiosMarcel - 26.06.2017

No matter which one is faster, don't use switch in such a situation.

Anyways, you can do benchmarks by running both very often, saving the start time and comparing it to the end time.


Re: Switch is faster than If? - rolex - 26.06.2017

Example:

I Have a JOB System.

for this:
PHP код:
if(JOB == 1) return SendClientMessage(playerid, -1"PizzaBoy");
if(
JOB == 2) return SendClientMessage(playerid, -1"Fisherman");
if(
JOB == 3) return SendClientMessage(playerid, -1"Lumberjacker");
if(
JOB == 4) return SendClientMessage(playerid, -1"Seller"); 
can i use this:
PHP код:
switch(JOB)
{
case 
1SendClientMessage(playerid, -1"PizzaBoy");
case 
2SendClientMessage(playerid, -1"Fisherman");
case 
3SendClientMessage(playerid, -1"Lumberjacker");
case 
4SendClientMessage(playerid, -1"Seller");

right?


Re: Switch is faster than If? - jlalt - 26.06.2017

Switch and if got their own usage.
If you use if in switch place it will be slow and if you use switch in if place it will be slow.

How?
Lets say you need to check for 1 and 3000 by switch you do
PHP код:
switch(var)
{
    case 
1..3000:
    {
    }
    default:
    {
    }

At this case switch used in if place and will be slow so have to use
PHP код:
if(< var < 3001)
{


So we get to this point switch is faster when switch is needed ( your above post declears switch example )
And if is faster when if is needed.


Re: Switch is faster than If? - rolex - 26.06.2017

So..

i know if usage, but sometimes i can use switch in place of if (like my example above) and both of these two have same ticktime to process, right?


Re: Switch is faster than If? - jlalt - 26.06.2017

Quote:
Originally Posted by rolex
Посмотреть сообщение
So..

i know if usage, but sometimes i can use switch in place of if (like my example above)
exactly D:
Quote:
Originally Posted by rolex
Посмотреть сообщение
both of these two have same ticktime to process, right?
Didn't get what you mean : o, mind explaining?


Re: Switch is faster than If? - rolex - 26.06.2017

In my example above, switch and if have same benchmark!?


Re: Switch is faster than If? - OneDay - 26.06.2017

why worry about this not real problem? did you profile to see that this was the real problem? no i bet you dont! other code is a problem not this so do not worry


Re: Switch is faster than If? - rolex - 26.06.2017

This is not a "problem" for me, and i never said that.

It's just a question, and aswered!


Thank you'all.