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 
01000000i++)
    {
        if(
== 1)
        {
            
0;
        }
        else if(
== 2)
        {
            
1;
        }
        else if(
== 3)
        {
            
2;
        }
        else if(
== 4)
        {
            
3;
        }
        else if(
== 5)
        {
            
4;
        }
        else if(
== 6)
        {
            
5;
        }
        else if(
== 7)
        {
            
6;
        }
        else if(
== 8)
        {
            
7;
        }
        else
        {
            
8;
        }
    }
    new 
took GetTickCount() - tick;
    
printf("1. Took %dms | Average: %f ms pertime"took, (took 1000000));
    
    
0;
    
tick GetTickCount();
    for(new 
01000000i++)
    {
        switch(
i)
        {
            case 
1:
            {
                
0;
            }
               case 
2:
            {
                
1;
            }
            case 
3:
            {
                
2;
            }
            case 
4:
            {
                
3;
            }
            case 
5:
            {
                
4;
            }
            case 
6:
            {
                
5;
            }
            case 
7:
            {
                
6;
            }
            case 
8:
            {
                
7;
            }
            default:
            {
                
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 Average0.000000 ms pertime
2. Took 126ms 
Average0.000000 ms pertime
V

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 
01000000i++)
    {
        if(
>= && <= 100)
        {
            
0;
        }
        else if(
>= 500 && <= 1000)
        {
            
1;
        }
        else if(
1000 && <= 1500)
        {
            
2;
        }
        else
        {
            
8;
        }
    }
    new 
took GetTickCount() - tick;
    
printf("1. Took %dms | Average: %f ms pertime"took, (took 1000000));
    
    
0;
    
tick GetTickCount();
    for(new 
01000000i++)
    {
        switch(
i)
        {
            case 
1..100:
            {
                
0;
            }
               case 
500..1000:
            {
                
1;
            }
            case 
1001..1500:
            {
                
2;
            }
            default:
            {
                
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 Average0.000000 ms pertime
2. Took 822ms 
Average0.000000 ms pertime
V

[ 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 Average0.000000 ms pertime
2. Took 507ms 
Average0.000000 ms pertime
V

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)