Raised This Month: $ Target: $400
 0% 

optimising this "if"


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Speed!
BANNED
Join Date: Jan 2009
Old 02-05-2009 , 12:39   optimising this "if"
Reply With Quote #1

i know that this is terribly unoptimised, and i have to execute this veerryyy often.
PHP Code:
static level
    
if (g_ammopacks[id] < 50)
        
level 1
    
else if (g_ammopacks[id] > 50 && g_ammopacks[id] < 125)
        
level 2
    
else if (g_ammopacks[id] > 125 && g_ammopacks[id] < 450)
        
level 3
    
else if (g_ammopacks[id] > 450 && g_ammopacks[id] < 800)
        
level 4
    
else if (g_ammopacks[id] > 800 && g_ammopacks[id] < 1250)
        
level 5
    
else if (g_ammopacks[id] > 1250 && g_ammopacks[id] < 2500)
        
level 6
    
else if (g_ammopacks[id] > 2500 && g_ammopacks[id] < 5000)
        
level 7
    
else if (g_ammopacks[id] > 5000 && g_ammopacks[id] < 10000)
        
level 8
    
else if (g_ammopacks[id] > 10000 && g_ammopacks[id] < 20000)
        
level 9
    
else if (g_ammopacks[id] > 20000)
        
level 10 
is this possible with a switch of something better?
Speed! is offline
Old 02-05-2009, 12:40
Dores
This message has been deleted by Dores.
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 02-05-2009 , 12:56   Re: optimising this "if"
Reply With Quote #3

Code:
    switch ( g_ammopacks[id] )     {         case 0     .. 49    : level = 1;         case 50    .. 124   : level = 2;         case 125   .. 449   : level = 3;         case 450   .. 799   : level = 4;         case 800   .. 1249  : level = 5;         case 1250  .. 2499  : level = 6;         case 2500  .. 4999  : level = 7;         case 5000  .. 9999  : level = 8;         case 10000 .. 19999 : level = 9;         default             : level = 10;     }
Arkshine is offline
Speed!
BANNED
Join Date: Jan 2009
Old 02-05-2009 , 13:03   Re: optimising this "if"
Reply With Quote #4

Quote:
Originally Posted by arkshine View Post
Code:
    switch ( g_ammopacks[id] )     {         case 0     .. 49    : level = 1;         case 50    .. 124   : level = 2;         case 125   .. 449   : level = 3;         case 450   .. 799   : level = 4;         case 800   .. 1249  : level = 5;         case 1250  .. 2499  : level = 6;         case 2500  .. 4999  : level = 7;         case 5000  .. 9999  : level = 8;         case 10000 .. 19999 : level = 9;         default             : level = 10;     }
thanks it worked +k

Last edited by Speed!; 02-05-2009 at 13:13.
Speed! is offline
YamiKaitou
Has a lovely bunch of coconuts
Join Date: Apr 2006
Location: Texas
Old 02-05-2009 , 13:00   Re: optimising this "if"
Reply With Quote #5

PHP Code:
    static level
    
if (g_ammopacks[id] < 50)
        
level 1
    
else if (g_ammopacks[id] < 125)
        
level 2
    
else if (g_ammopacks[id] < 450)
        
level 3
    
else if (g_ammopacks[id] < 800)
        
level 4
    
else if (g_ammopacks[id] < 1250)
        
level 5
    
else if (g_ammopacks[id] < 2500)
        
level 6
    
else if (g_ammopacks[id] < 5000)
        
level 7
    
else if (g_ammopacks[id] < 10000)
        
level 8
    
else if (g_ammopacks[id] < 20000)
        
level 9
    
else
        
level 10 
If you must use the if, use this. This is much better than how you had it.
__________________
ProjectYami Laboratories

I do not browse the forums regularly anymore. If you need me for anything (asking questions or anything else), then PM me (be descriptive in your PM, message containing only a link to a thread will be ignored).
YamiKaitou is offline
Speed!
BANNED
Join Date: Jan 2009
Old 02-05-2009 , 13:15   Re: optimising this "if"
Reply With Quote #6

now instead of being 206kb weight it is 950 weight :S is that normal?
Speed! is offline
Old 02-05-2009, 14:01
xPaw
This message has been deleted by xPaw.
Drak
Veteran Member
Join Date: Jul 2005
Old 02-06-2009 , 16:03   Re: optimising this "if"
Reply With Quote #8

Yes. It doesn't mean that it's any less "optimized"
Switch statements are always perfered over if..else
__________________
Oh yeah
Drak is offline
Send a message via MSN to Drak
jim_yang
Veteran Member
Join Date: Aug 2006
Old 02-07-2009 , 02:51   Re: optimising this "if"
Reply With Quote #9

Code:
#include <amxmodx>
 
#define PLUGIN    "Test"
#define AUTHOR    "Jim"
#define VERSION    "1.0"
 
public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR)
}
 
public func1()
{
    new i
    switch(i)
    {
        case 1 .. 10: i = 2
        case 11 .. 20: i = 3
        case 21 .. 30: i = 4
        default: i = 5;
    }
}
 
public func2()
{
    new i
    if(i <= 10)
        i = 2
    else if(i <=20)
        i = 3
    else if(i <= 30)
        i = 4
    else i = 5
}
 
.DATA
    str_000 string "Test"
    str_001 string "1.0"
    str_002 string "Jim"

.CODE
    halt                 0x0
PROC  plugin_init
    break     
    break     
    push.c              0x24
    push.c              0x14
    push.c               0x0
    push.c               0xC
    sysreq.c     register_plugin
    stack               0x10
    zero.pri  
    retn      
ENDP
PROC  func1
    break     
    break     
    push.c               0x0
    break     
    load.s.pri          -0x4
    switch         switch_00
 case_00_00:
    break     
    const.pri            0x2
    stor.s.pri          -0x4
    jump           jump_0000
 case_00_10:
    break     
    const.pri            0x3
    stor.s.pri          -0x4
    jump           jump_0000
 case_00_20:
    break     
    const.pri            0x4
    stor.s.pri          -0x4
    jump           jump_0000
 casetble_00:
    break     
    const.pri            0x5
    stor.s.pri          -0x4
    jump           jump_0000
 switch_00:
    casetbl             0x1E   casetble_00
     case                0x1    case_00_00
     case                0x2    case_00_00
     case                0x3    case_00_00
     case                0x4    case_00_00
     case                0x5    case_00_00
     case                0x6    case_00_00
     case                0x7    case_00_00
     case                0x8    case_00_00
     case                0x9    case_00_00
     case                0xA    case_00_00
     case                0xB    case_00_10
     case                0xC    case_00_10
     case                0xD    case_00_10
     case                0xE    case_00_10
     case                0xF    case_00_10
     case               0x10    case_00_10
     case               0x11    case_00_10
     case               0x12    case_00_10
     case               0x13    case_00_10
     case               0x14    case_00_10
     case               0x15    case_00_20
     case               0x16    case_00_20
     case               0x17    case_00_20
     case               0x18    case_00_20
     case               0x19    case_00_20
     case               0x1A    case_00_20
     case               0x1B    case_00_20
     case               0x1C    case_00_20
     case               0x1D    case_00_20
     case               0x1E    case_00_20
 jump_0000:
    stack                0x4
    zero.pri  
    retn      
ENDP
PROC  func2
    break     
    break     
    push.c               0x0
    break     
    load.s.pri          -0x4
    const.alt            0xA
    jsgrtr         jump_0004
    break     
    const.pri            0x2
    stor.s.pri          -0x4
    jump           jump_0005
 jump_0004:
    break     
    load.s.pri          -0x4
    const.alt           0x14
    jsgrtr         jump_0006
    break     
    const.pri            0x3
    stor.s.pri          -0x4
    jump           jump_0005
 jump_0006:
    break     
    load.s.pri          -0x4
    const.alt           0x1E
    jsgrtr         jump_0008
    break     
    const.pri            0x4
    stor.s.pri          -0x4
    jump           jump_0005
 jump_0008:
    break     
    const.pri            0x5
    stor.s.pri          -0x4
 jump_0005:
    stack                0x4
    zero.pri  
    retn      
ENDP
when the case range is too large , I'd prefer Yami's way
reason is showed above
__________________
Project : CSDM all in one - 99%
<team balancer#no round end#entity remover#quake sounds#fake full#maps management menu#players punishment menu#no team flash#colored flashbang#grenade trails#HE effect#spawn protection#weapon arena#weapon upgrade#auto join#no weapon drop#one name>
jim_yang 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:51.


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