Raised This Month: $ Target: $400
 0% 

Optimizng a Switch() statement


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Dores
Veteran Member
Join Date: Jun 2008
Location: You really don't wanna k
Old 02-02-2009 , 17:41   Optimizng a Switch() statement
Reply With Quote #1

There must be a way to optimize this, I just can't think of it. :S

Code:
static arg[2], nade, class[21]; read_argv(1, arg, 1); switch(arg[0]) {     case 'f':     {         if(!get_pcvar_num(g_pFlash))         {                         return PLUGIN_HANDLED;         }                 nade = CSW_FLASHBANG;                 copy(class, 20, "weapon_flashbang");     }         case 'h':     {         if(!get_pcvar_num(g_pHE))         {                         return PLUGIN_HANDLED;         }                 nade = CSW_HEGRENADE;                 copy(class, 20, "weapon_hegrenade");     }         case 's':     {         if(!get_pcvar_num(g_pSmoke))         {                         return PLUGIN_HANDLED;         }                 nade = CSW_SMOKEGRENADE;                 copy(class, 20, "weapon_smokegrenade");     } }

Thanks in advance.
__________________
O o
/Ż________________________
| IMMA FIRIN' MAH LAZOR!!!
\_ŻŻŻ
Dores is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 02-02-2009 , 17:51   Re: Optimizng a Switch() statement
Reply With Quote #2

Code:
enum {     NADE_HE,     NADE_FL,     NADE_SM,         NADE_TOTAL }; new const g_nade_ids[NADE_TOTAL] = {     CSW_HEGRENADE,     CSW_FLASHBANG,     CSW_SMOKEGRENADE, }; new const g_nade_weaponnames[NADE_TOTAL][] = {     "weapon_hegrenade",     "weapon_flashbang",     "weapon_smokegrenade" }; new g_pcvar_nade[NADE_TOTAL]; public plugin_init() {     g_pcvar_nade[NADE_HE] = register_cvar("he_nade", "1");     g_pcvar_nade[NADE_FL] = register_cvar("fl_nade", "1");     g_pcvar_nade[NADE_SM] = register_cvar("sm_nade", "1"); } public function() {     static arg[2], nade;     read_argv(1, arg, 1);         switch( arg[0] )     {         case 'h': nade = NADE_HE;         case 'f': nade = NADE_FL;         case 's': nade = NADE_SM;                 default: return PLUGIN_HANDLED;     }         if( !get_pcvar_num(g_pcvar_nade[nade]) ) return PLUGIN_HANDLED;         // CSW_* = g_nade_ids[nade]     // class = g_nade_weaponnames[nade] }
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Dores
Veteran Member
Join Date: Jun 2008
Location: You really don't wanna k
Old 02-02-2009 , 18:03   Re: Optimizng a Switch() statement
Reply With Quote #3

You have some mistakes:
Code:
case 'h': nade = NADE_HE; case 'f': nade = NADE_FL; case 's': nade = NADE_SM;  ---> case 'h': nade = g_nade_ids[NADE_HE]; case 'f': nade = g_nade_ids[NADE_FL]; case 's': nade = g_nade_ids[NADE_SM]; // Also: if( !get_pcvar_num(g_pcvar_nade[nade]) ) return PLUGIN_HANDLED; // nade = grenades ids, and g_pcvar_nade is limited to 0, 1 and 2.
See why I got in trouble? ^^

+Karma when I can, still not solved.

EDIT: Never mind, I'll just use your method but instead of 'h', 'f' and 's' I'll use 0, 1 and 2.
Thanks.
__________________
O o
/Ż________________________
| IMMA FIRIN' MAH LAZOR!!!
\_ŻŻŻ

Last edited by Dores; 02-02-2009 at 18:12.
Dores is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 02-02-2009 , 18:09   Re: Optimizng a Switch() statement
Reply With Quote #4

Quote:
Originally Posted by Dores View Post
By the way, you have a mistake:
Code:
case 'h': nade = NADE_HE; case 'f': nade = NADE_FL; case 's': nade = NADE_SM;  ---> case 'h': nade = g_nade_ids[NADE_HE]; case 'f': nade = g_nade_ids[NADE_FL]; case 's': nade = g_nade_ids[NADE_SM];
Make sure that you retrieve the classname (g_nade_weaponnames) in the same switch statement if you are assigning "nade" the CSW_* id, instead of the enum id.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 02-02-2009 , 18:10   Re: Optimizng a Switch() statement
Reply With Quote #5

No it's not a mistake. Anyway, I'm sure if its way is really an optimization. Depending if this function is called often or not. Your way is enough, except maybe you could avoid to use copy.
Arkshine is offline
Dores
Veteran Member
Join Date: Jun 2008
Location: You really don't wanna k
Old 02-02-2009 , 23:54   Re: Optimizng a Switch() statement
Reply With Quote #6

Oh, I get it now!
Thanks both!

And arkshine, it's kinda hard to say how often this function is called, but an optimization would be good anyways.
And yeah, I won't use copy.

+Karma(I still can't give you Exo).
__________________
O o
/Ż________________________
| IMMA FIRIN' MAH LAZOR!!!
\_ŻŻŻ
Dores is offline
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

Forum Jump


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


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