Switch or if else for large number
#1

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

Just do a benchmark, to see which is faster(if any)

Also i think case looks cleaner
Reply
#3

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 
= -1;
while(
score >= playerTitles[1][requiredScore] && sizeof playerTitles 1) { i++; }
SetPlayerTitle(playeridplayerTitles[i][title]); 
Something along those lines.
Reply
#4

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

Switch is 458865322467789986 times faster no doubt.

Even one if-then is really slow.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)