Raised This Month: $32 Target: $400
 8% 

Switch or Array and if


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
zeroibis
Veteran Member
Join Date: Jun 2007
Old 04-23-2012 , 22:37   Switch or Array and if
Reply With Quote #1

I am wondering if it is faster to use a bit more code and complexity to implement a series of switch statements instead of a single if. Below is a simple example:

Code:
new Float:arr[3]={0.0,0.1,0.2}

if(arr[ranposition]<=rand(0,1)
{
run
}
or

Code:
new position = ranposition
switch(position)
{
case 0:
{
new chance=ranint(0,100)
switch(case)
{
case 0...10
{
run
}
}
}
ect...
}
Basically you replace all ranges of values with switch statements. It would be a pain in the ass to code but would it actually run faster!?
__________________
zeroibis is offline
napalm00
Veteran Member
Join Date: Jun 2011
Location: Italy, sadly
Old 04-24-2012 , 00:04   Re: Switch or Array and if
Reply With Quote #2

Switches are faster than if statements, so yes.
__________________
napalm00 is offline
zeroibis
Veteran Member
Join Date: Jun 2007
Old 04-24-2012 , 00:25   Re: Switch or Array and if
Reply With Quote #3

Yes under normal conditions however here it is a series of switch statements vs a single if statement. Would even a large number of switches still be faster than a single if!?

This I find to be more of a question of how big of a difference there really is between the two in sm.
__________________
zeroibis is offline
thetwistedpanda
Good Little Panda
Join Date: Sep 2008
Old 04-24-2012 , 00:29   Re: Switch or Array and if
Reply With Quote #4

You should be able to use the profiler to determine which one is faster.
__________________
thetwistedpanda is offline
napalm00
Veteran Member
Join Date: Jun 2011
Location: Italy, sadly
Old 04-24-2012 , 00:32   Re: Switch or Array and if
Reply With Quote #5

Switches use a lookup table (or a hash list), which *should* make them faster in any condition. It's like a normal array vs a trie array, if you have just a few items you won't notice any difference, but if you have a lot, then using a switch is the best option.
That said, I believe using if/else statements is better for a cleaner code, but we're talking about performance here.
__________________

Last edited by napalm00; 04-24-2012 at 00:34.
napalm00 is offline
zeroibis
Veteran Member
Join Date: Jun 2007
Old 04-24-2012 , 10:01   Re: Switch or Array and if
Reply With Quote #6

Well if anything I suppose it is time to play around with the profiler a bit then.
__________________
zeroibis is offline
Dr!fter
The Salt Boss
Join Date: Mar 2007
Old 04-24-2012 , 12:23   Re: Switch or Array and if
Reply With Quote #7

I used this plugin to test the difference.

PHP Code:
//Yar
#include <sourcemod>

public OnPluginStart()
{
    new 
Float:time GetEngineTime();
    
PrintToServer("Start time for if loop is %f"time)
    for(new 
0<= 100000i++)
    {
        new 
random GetRandomInt(0100);
        if(
random <= 5)
        {
            
PrintToChatAll("Yay!")
        }
    }
    
PrintToServer("Total time for if was %f"GetEngineTime()-time)
    
time GetEngineTime();
    
PrintToServer("Start time for switch loop is %f"time)
    for(new 
0<= 100000i++)
    {
        new 
random GetRandomInt(0,100);
        switch(
random)
        {
            
/*case 0...5: //This dosnt work!
            {
                PrintToChatAll("Yay!")
            }*/
            
case 012345:
            {
                
PrintToChatAll("Yay!")
            }
        }
    }
    
PrintToServer("Total time for switch was %f"GetEngineTime()-time)

Output
Code:
Start time for if loop is 1.846865
Total time for if was 0.009515
Start time for switch loop is 1.856752
Total time for switch was 0.009230
As you can see no huge difference or even noticeable difference. This probably isnt the best method to test but. To be fair their is very little need to worry about if or switch.

Last edited by Dr!fter; 04-24-2012 at 12:26.
Dr!fter is offline
RedSword
SourceMod Plugin Approver
Join Date: Mar 2006
Location: Quebec, Canada
Old 04-24-2012 , 14:07   Re: Switch or Array and if
Reply With Quote #8

I do believe that when comparing using if or switch would in assembly would both do something like :

- put a var in a register
- put a constant in another
- compare the two

but whenever comparing more than once, the if would do :

- put a var in a register
- put a constant in another
- compare
- REPEAT

while switch would do :

-put a var in a register
-put a constant in another
-compare
-REPEAT from step 2 (keeps the same var)

However compilers does optimize it (I've no idea about pawn; but since it seems less important (then let's say Visual Studio compiler), I'd guess it is not optimized; I'd have to look at the compiler's code however ). Also, I've no idea about loops; as I'm talking here about consecutive comparison (if, elseif, etc.)
__________________
My plugins :
Red Maze
Afk Bomb
RAWR (per player/rounds Awp Restrict.)
Kill Assist
Be Medic

You can also Donate if you appreciate my work

Last edited by RedSword; 04-24-2012 at 14:08.
RedSword is offline
Reply



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

Forum Jump


All times are GMT -4. The time now is 01:58.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode