Switch or if else for large number - 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 or if else for large number (
/showthread.php?tid=625057)
Switch or if else for large number -
Nubik - 27.12.2016
Is there any downsides of using this
Code:
if(Score >= 0)
else if(Score > 49 && Score < 100)
else if(Score > 99 && Score < 250)
else if(Score > 249 && Score < 500)
else if(Score > 499 && Score < 1000)
else if(Score > 999 && Score < 2500)
else if(Score > 2499 && Score < 5000)
else if(Score > 4999)
Than this
Code:
switch(Score)
{
case 0..49:
case 50..99:
case 100..249:
case 250..499:
case 500..999:
case 1000..1499: // and so on
}
I read somewhere that switch isn't for large number of cases not sure if switching 5000 cases would be ok or im better off with if else.
Re: Switch or if else for large number -
BiosMarcel - 27.12.2016
Just do a benchmark, to see which is faster(if any)
Also i think case looks cleaner
Re: Switch or if else for large number -
Vince - 27.12.2016
Depends on what you're doing inside those blocks, honestly. But you can probably use an array and a loop in some way. Be it to store some titles or reward amounts or ...
PHP Code:
enum E_PLAYER_TITLES {
requiredScore,
title[24]
}
new playerTitles[E_PLAYER_TITLES] = {
{0, "Beginner"},
{50, "Intermediate"},
{100, "Expert"}
};
new i = -1;
while(score >= playerTitles[i + 1][requiredScore] && i < sizeof playerTitles - 1) { i++; }
SetPlayerTitle(playerid, playerTitles[i][title]);
Something along those lines.
Re: Switch or if else for large number -
Hansrutger - 27.12.2016
I go with switch case always because I can read it easier, but yea go and read about
https://sampwiki.blast.hk/wiki/GetTickCount to see which one is faster. They are more or less the same thing in pawn though as a switch in pawn isn't a switch but actually an else if.
Re: Switch or if else for large number -
SickAttack - 27.12.2016
Switch is 458865322467789986 times faster no doubt.
Even one if-then is really slow.