AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   attempting to optimize code (https://forums.alliedmods.net/showthread.php?t=283328)

sirerick 05-31-2016 08:41

attempting to optimize code
 
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")         
    } 


gabuch2 05-31-2016 08:56

Re: attempting to optimize code
 
Why are you checking g_isalive in every condition?

OciXCrom 05-31-2016 09:23

Re: attempting to optimize code
 
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")
            }
        }
    }



sirerick 05-31-2016 09:26

Re: attempting to optimize code
 
Quote:

Originally Posted by Shattered Heart Lynx (Post 2423482)
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 (Post 2423484)
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

siriusmd99 05-31-2016 09:50

Re: attempting to optimize code
 
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



JusTGo 05-31-2016 10:24

Re: attempting to optimize code
 
Quote:

Originally Posted by siriusmd99 (Post 2423497)
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.

siriusmd99 05-31-2016 11:21

Re: attempting to optimize code
 
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.

JusTGo 05-31-2016 11:37

Re: attempting to optimize code
 
Quote:

Originally Posted by siriusmd99 (Post 2423515)
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

HamletEagle 05-31-2016 12:49

Re: attempting to optimize code
 
On usual functions, caching alive status is useless. It's somehow usefull on per frame forwards.

klippy 05-31-2016 12:56

Re: attempting to optimize code
 
Quote:

Originally Posted by OciXCrom (Post 2423484)
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 (Post 2423515)
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.


All times are GMT -4. The time now is 18:39.

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