switch() vs if/else if
#1

Hi guys, I was wondering if (if/else if) in larger callbacks like OnDialogResponse with 400 if/else if statements caused more lag than switch. Is it really more efficient to use switch or is is there no difference?
Reply
#2

You won't really sense the difference under ondialogresponse, look at the speed test:

PHP Code:
#include <a_samp>
public OnFilterScriptInit()
{
    new 
v;
    new 
tick = GetTickCount();
    for(new 
i = 0; i < 1000000; i++)
    {
        if(
i == 1)
        {
            
v = 0;
        }
        else if(
i == 2)
        {
            
v = 1;
        }
        else if(
i == 3)
        {
            
v = 2;
        }
        else if(
i == 4)
        {
            
v = 3;
        }
        else if(
i == 5)
        {
            
v = 4;
        }
        else if(
i == 6)
        {
            
v = 5;
        }
        else if(
i == 7)
        {
            
v = 6;
        }
        else if(
i == 8)
        {
            
v = 7;
        }
        else
        {
            
v = 8;
        }
    }
    new 
took = GetTickCount() - tick;
    
printf("1. Took %dms | Average: %f ms pertime", took, (took / 1000000));
    
    
v = 0;
    
tick = GetTickCount();
    for(new 
i = 0; i < 1000000; i++)
    {
        switch(
i)
        {
            case 
1:
            {
                
v = 0;
            }
               case 
2:
            {
                
v = 1;
            }
            case 
3:
            {
                
v = 2;
            }
            case 
4:
            {
                
v = 3;
            }
            case 
5:
            {
                
v = 4;
            }
            case 
6:
            {
                
v = 5;
            }
            case 
7:
            {
                
v = 6;
            }
            case 
8:
            {
                
v = 7;
            }
            default:
            {
                
v = 8;
            }
        }
    }
    
took = GetTickCount() - tick;
    
printf("2. Took %dms | Average: %f ms pertime", took, (took / 1000000));
    
printf("V: %d", v);
} 
-> Result:
PHP Code:
1. Took 433ms | Average: 0.000000 ms pertime
2. Took 126ms 
| Average: 0.000000 ms pertime
V
: 8 
Yes switch is faster than if but not that much that you will sense by small numbers of checks / calls.

Can switch be slower than if?
Yes it can.

->

PHP Code:
#include <a_samp>
public OnFilterScriptInit()
{
    new 
v;
    new 
tick = GetTickCount();
    for(new 
i = 0; i < 1000000; i++)
    {
        if(
i >= 1 && i <= 100)
        {
            
v = 0;
        }
        else if(
i >= 500 && i <= 1000)
        {
            
v = 1;
        }
        else if(
i > 1000 && i <= 1500)
        {
            
v = 2;
        }
        else
        {
            
v = 8;
        }
    }
    new 
took = GetTickCount() - tick;
    
printf("1. Took %dms | Average: %f ms pertime", took, (took / 1000000));
    
    
v = 0;
    
tick = GetTickCount();
    for(new 
i = 0; i < 1000000; i++)
    {
        switch(
i)
        {
            case 
1..100:
            {
                
v = 0;
            }
               case 
500..1000:
            {
                
v = 1;
            }
            case 
1001..1500:
            {
                
v = 2;
            }
            default:
            {
                
v = 8;
            }
        }
    }
    
took = GetTickCount() - tick;
    
printf("2. Took %dms | Average: %f ms pertime", took, (took / 1000000));
    
printf("V: %d", v);
} 
Result ->

PHP Code:
1. Took 313ms | Average: 0.000000 ms pertime
2. Took 822ms 
| Average: 0.000000 ms pertime
V
: 8 
[ fixed a mistake on the second speed test, I've wrote one dot in switch case statement rather than two, it became slower even. ]
Reply
#3

Quote:
Originally Posted by jlalt
View Post
Result ->

PHP Code:
1. Took 314ms | Average: 0.000000 ms pertime
2. Took 507ms 
| Average: 0.000000 ms pertime
V
: 8 
I think the last one was slower because the compiler has to "expand" the cases, which results to aprox 1500 cases... So you are comparing 3 ifs with 1500cases... Correct me if I am wrong.
Reply
#4

Quote:
Originally Posted by 10MIN
View Post
I think the last one was slower because the compiler has to "expand" the cases, which results to aprox 1500 cases... So you are comparing 3 ifs with 1500cases... Correct me if I am wrong.
exactly, that's the position when if becomes handy.
Reply
#5

Quote:
Originally Posted by 10MIN
View Post
I think the last one was slower because the compiler has to "expand" the cases, which results to aprox 1500 cases... So you are comparing 3 ifs with 1500cases... Correct me if I am wrong.
Right - Y_Less has explained this before in 'minor coding queries' topic.
Reply
#6

These are some pretty strong facts, thank you all for your time and rendition.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)