SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 13/06/2018, 10:42 AM   #1
ElMaestro123
Big Clucker
 
Join Date: Feb 2017
Location: Stuck in the middle of nowhere.
Posts: 106
Reputation: 0
Thumbs up switch() vs if/else if

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?
ElMaestro123 is offline   Reply With Quote
Old 13/06/2018, 10:56 AM   #2
jlalt
Banned
 
Join Date: Oct 2014
Location: Iran
Posts: 1,295
Reputation: 366
Default Re: switch() vs if/else if

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. ]

Last edited by jlalt; 13/06/2018 at 02:54 PM.
jlalt is offline   Reply With Quote
Old 13/06/2018, 11:17 AM   #3
10MIN
Huge Clucker
 
10MIN's Avatar
 
Join Date: Jun 2017
Location: Clock4ever
Posts: 334
Reputation: 28
Default Re: switch() vs if/else if

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.
__________________
**signature**
10MIN is offline   Reply With Quote
Old 13/06/2018, 11:32 AM   #4
jlalt
Banned
 
Join Date: Oct 2014
Location: Iran
Posts: 1,295
Reputation: 366
Default Re: switch() vs if/else if

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.
jlalt is offline   Reply With Quote
Old 13/06/2018, 11:34 AM   #5
Logic_
High-roller
 
Logic_'s Avatar
 
Join Date: Jun 2015
Location: 666
Posts: 1,493
Reputation: 267
Default Re: switch() vs if/else if

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.
__________________
Premium quality and reasonable pricing game Server hosting
Logic_ is offline   Reply With Quote
Old 13/06/2018, 04:37 PM   #6
ElMaestro123
Big Clucker
 
Join Date: Feb 2017
Location: Stuck in the middle of nowhere.
Posts: 106
Reputation: 0
Default Re: switch() vs if/else if

These are some pretty strong facts, thank you all for your time and rendition.
ElMaestro123 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
IF to SWITCH Hazliu Scripting Help 9 27/04/2018 02:08 PM
How to switch from R6 to R39? Barnwell Scripting Help 7 26/10/2016 02:31 AM
switch max value 194? Spenker Scripting Help 1 24/07/2016 08:22 AM
Switch inside switch? Lajko1 Scripting Help 2 03/05/2016 05:37 PM
Switch Syntax Scripting Help 15 10/04/2013 03:06 PM


All times are GMT. The time now is 12:01 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.