Raised This Month: $51 Target: $400
 12% 

attempting to optimize code


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
sirerick
Senior Member
Join Date: Jul 2012
Location: Venezuela
Old 05-31-2016 , 08:41   attempting to optimize code
Reply With Quote #1

Is this the best way to elbow this code?

PHP Code:
if (g_isalive[id] && (get_user_team(id) == ) && (g_level[id] > 99) && (g_level[id] <= 199))
    {
        
cs_set_player_model(id"Model_1_CT")         
    }
    else if (
g_isalive[id] && (cs_get_user_team(id) == CS_TEAM_T ) && (g_level[id] > 99) && (g_level[id] <= 199))
    {
        
cs_set_player_model(id"Model_1_T")         
    }
    if (
g_isalive[id] && (get_user_team(id) == ) && (g_level[id] == 200))
    {
        
cs_set_player_model(id"Model_2_CT")         
    }
    else if (
g_isalive[id] && (cs_get_user_team(id) == CS_TEAM_T ) && (g_level[id] == 200))
    {
        
cs_set_player_model(id"Model_2_T")         
    } 
sirerick is offline
gabuch2
AlliedModders Donor
Join Date: Mar 2011
Location: Chile
Old 05-31-2016 , 08:56   Re: attempting to optimize code
Reply With Quote #2

Why are you checking g_isalive in every condition?
__________________
gabuch2 is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 05-31-2016 , 09:23   Re: attempting to optimize code
Reply With Quote #3

Not even close to good. This is somewhat better:

PHP Code:
if(g_isalive[id])
{
    switch(
g_level[id])
    {
        case 
100 .. 199:
        {
            switch(
get_user_team(id))
            {
                case 
1cs_set_user_model(id"Model_1_T")
                case 
2cs_set_user_model(id"Model_1_CT")
            }
        }
        case 
200:
        {
            switch(
get_user_team(id))
            {
                case 
1cs_set_user_model(id"Model_2_T")
                case 
2cs_set_user_model(id"Model_2_CT")
            }
        }
    }

OciXCrom is offline
Send a message via Skype™ to OciXCrom
sirerick
Senior Member
Join Date: Jul 2012
Location: Venezuela
Old 05-31-2016 , 09:26   Re: attempting to optimize code
Reply With Quote #4

Quote:
Originally Posted by Shattered Heart Lynx View Post
Why are you checking g_isalive in every condition?
new on pawn but i stop learning and I'm starting again from scratch

Quote:
Originally Posted by OciXCrom View Post
Not even close to good. This is somewhat better:

PHP Code:
if(g_isalive[id])
{
    switch(
g_level[id])
    {
        case 
100 .. 199:
        {
            switch(
get_user_team(id))
            {
                case 
1cs_set_user_model(id"Model_1_T")
                case 
2cs_set_user_model(id"Model_1_CT")
            }
        }
        case 
200:
        {
            switch(
get_user_team(id))
            {
                case 
1cs_set_user_model(id"Model_2_T")
                case 
2cs_set_user_model(id"Model_2_CT")
            }
        }
    }

ty so much
sirerick is offline
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 05-31-2016 , 09:50   Re: attempting to optimize code
Reply With Quote #5

No , it's not correct.
You are checking if equal to 200 but it can be more than 200 . He will have model only when has level 200 but no 201...
And use cs_get_user_team instead, it returns correct teams.
And why do you use g_isalive? Use directly is_user_alive , it's library native and is faster then plugin array.


PHP Code:
if (is_user_alive(id))
{
  if(
g_level[id] < 99)
  return;
  new 
pmodel[11];
  
formatex(pmodel7"Model_%s"g_level[id] >= 200 "2" "1" )
  
  switch(
cs_get_user_team(id))
  {
    case 
CS_TEAM_Tadd(pmodel9"_T");
    case 
CS_TEAM_CTadd(pmodel10"_CT");
    default: return;
  }
  
cs_set_player_model(idpmodel

siriusmd99 is offline
JusTGo
Veteran Member
Join Date: Mar 2013
Old 05-31-2016 , 10:24   Re: attempting to optimize code
Reply With Quote #6

Quote:
Originally Posted by siriusmd99 View Post
And why do you use g_isalive? Use directly is_user_alive , it's library native and is faster then plugin array.
not true cashing value is faster.
__________________
JusTGo is offline
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 05-31-2016 , 11:21   Re: attempting to optimize code
Reply With Quote #7

yes?

how plugin works:

death - is_alive[id] = false;
disconnect is_alive[id] = false;
spawn - is_alive[id] = true;

native_is_user_alive(id)
return is_alive(id)


when you use is_user_alive(id), don't you get already cached value?

the thing is that when you use is_alive , you acces global array from plugin but when you call native is_user_alive, you call array from module, which is faster.

Last edited by siriusmd99; 05-31-2016 at 11:25.
siriusmd99 is offline
JusTGo
Veteran Member
Join Date: Mar 2013
Old 05-31-2016 , 11:37   Re: attempting to optimize code
Reply With Quote #8

Quote:
Originally Posted by siriusmd99 View Post
yes?

how plugin works:

death - is_alive[id] = false;
disconnect is_alive[id] = false;
spawn - is_alive[id] = true;

native_is_user_alive(id)
return is_alive(id)


when you use is_user_alive(id), don't you get already cached value?

the thing is that when you use is_alive , you acces global array from plugin but when you call native is_user_alive, you call array from module, which is faster.
https://forums.alliedmods.net/showthread.php?t=162735
https://forums.alliedmods.net/showth...hlight=Caching
https://forums.alliedmods.net/showthread.php?t=88792
__________________
JusTGo is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 05-31-2016 , 12:49   Re: attempting to optimize code
Reply With Quote #9

On usual functions, caching alive status is useless. It's somehow usefull on per frame forwards.
__________________

Last edited by HamletEagle; 05-31-2016 at 12:49.
HamletEagle is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 05-31-2016 , 12:56   Re: attempting to optimize code
Reply With Quote #10

Quote:
Originally Posted by OciXCrom View Post
Not even close to good. This is somewhat better:

PHP Code:
if(g_isalive[id])
{
    switch(
g_level[id])
    {
        case 
100 .. 199:
        {
            switch(
get_user_team(id))
            {
                case 
1cs_set_user_model(id"Model_1_T")
                case 
2cs_set_user_model(id"Model_1_CT")
            }
        }
        case 
200:
        {
            switch(
get_user_team(id))
            {
                case 
1cs_set_user_model(id"Model_2_T")
                case 
2cs_set_user_model(id"Model_2_CT")
            }
        }
    }

Don't ever do something like "100 .. 199", as that actually creates 100 entries in the jump table of the switch. Having just a simple "if" will probably be faster there. It's okay to use it when you are dealing with small ranges, like 10-20 or whatever, but not with hundreds. I believe that's the reason Bailopan switched off that feature in SourcePawn.

Quote:
Originally Posted by siriusmd99 View Post
yes?

how plugin works:

death - is_alive[id] = false;
disconnect is_alive[id] = false;
spawn - is_alive[id] = true;

native_is_user_alive(id)
return is_alive(id)


when you use is_user_alive(id), don't you get already cached value?

the thing is that when you use is_alive , you acces global array from plugin but when you call native is_user_alive, you call array from module, which is faster.
While module (C++) code will most likely run faster than pawn code, native calling is what is expensive and slow, at least compared to retrieving a value from an array.

Last edited by klippy; 05-31-2016 at 12:57.
klippy 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 15:04.


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