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

Solved Need help with optimization


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
kww
Senior Member
Join Date: Feb 2021
Location: Russia
Old 02-12-2021 , 13:30   Need help with optimization
Reply With Quote #1

Hello guys! I need some help with optimizing my code. I mean "how i can optimize "if/else if" section"?
Also i wanted to check if players wrote something like "bfshbj" (random words or chracters) as argument then "client_print" from first condition will be executed.
Can someone help me, please?
PHP Code:
public xbhop_main(id)
{
    new 
x_arg[33], x_arg2[33]
    
read_argv(1x_argcharsmax(x_arg))
    
read_argv(2x_arg2charsmax(x_arg2))
    
    if (
equali(x_arg"")) //or can i use "if (!x_arg[0])" instead? It's working too, but is it good to use it instead of "equali"?
    
{
        
client_print(idprint_console"Usage: xbhop3 <help | argument>")
    }
    else if (
equali(x_arg"toggle"))
    {
        
toggle_hopper(id)
    }
    else if (
equali(x_arg"help"))
    {
        
xbhop_help(id)
    }
    else if (
equali(x_arg"autojump"))
    {
        if (
equali(x_arg2""))
            
client_print(idprint_console"Usage: xbhop3 autojump <1 | 0> (now %i)"autojump)
        else if (
equali(x_arg2"0"))
            
autojump 0
        
else if (equali(x_arg2"1"))
            
autojump 1
    
}
    
    return 
PLUGIN_HANDLED


Last edited by kww; 05-29-2021 at 06:31.
kww is offline
lexzor
Veteran Member
Join Date: Nov 2020
Old 02-12-2021 , 13:57   Re: Need help with optimization
Reply With Quote #2

first of all, use switch instead of if and strlen to see if there are arguments

https://www.amxmodx.org/api/string/strlen
lexzor is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 02-12-2021 , 14:04   Re: Need help with optimization
Reply With Quote #3

Quote:
Originally Posted by lexzor View Post
first of all, use switch instead of if and strlen to see if there are arguments

https://www.amxmodx.org/api/string/strlen
Switch with strings?!
"strlen" instead of not using a native at all?!
Don't try to help if you don't even know the basics of Pawn coding.

Quote:
if (equali(x_arg, "")) //or can i use "if (!x_arg[0])" instead? It's working too, but is it good to use it instead of "equali"?
Yes, it's better to use that instead of calling a native.

If you're using AMXX 1.9/1.10, you should use "read_argv_int" when working with numbers. Otherwise, convert the string to integer with "str_to_num" when dealing with them, and use "switch" in such cases.

You probably don't need 33 buffer size since the most you're using at the moment is 8.
__________________
OciXCrom is offline
Send a message via Skype™ to OciXCrom
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 02-12-2021 , 14:14   Re: Need help with optimization
Reply With Quote #4

It all depends on how often it is called.

In your case I'd say your code is fine. but !x_arg[0] or strlen(x_arg) is a good compliment.

Quote:
Originally Posted by OciXCrom View Post
You probably don't need 33 buffer size since the most you're using at the moment is 8.
*cough* +1
__________________

Last edited by Black Rose; 02-12-2021 at 14:18.
Black Rose is offline
kww
Senior Member
Join Date: Feb 2021
Location: Russia
Old 02-12-2021 , 14:17   Re: Need help with optimization
Reply With Quote #5

Quote:
Originally Posted by OciXCrom View Post
Yes, it's better to use that instead of calling a native.
native is "equali"?

Quote:
Originally Posted by OciXCrom View Post
If you're using AMXX 1.9/1.10, you should use "read_argv_int" when working with numbers. Otherwise, convert the string to integer with "str_to_num" when dealing with them, and use "switch" in such cases.
I'm using AMXX 1.8.2. Should i download latest version?

Quote:
Originally Posted by OciXCrom View Post
You probably don't need 33 buffer size since the most you're using at the moment is 8.
Maybe. I'll decrease it later

And I want to know what can i do with "if players typing something like "bfshbj" (random words or chracters) as argument". Can you suggest something please?

Last edited by kww; 02-12-2021 at 14:19.
kww is offline
lexzor
Veteran Member
Join Date: Nov 2020
Old 02-12-2021 , 14:17   Re: Need help with optimization
Reply With Quote #6

so we can't use switch like this?:

PHP Code:
new situations[][]=
{
"situation1",
"situation2",
"situation3"    
}

public 
test(id)
{
new 
args[10]
read_argv(idargssizeof(args))

for (new 
0sizeof(situations); i++)
{
    if(!
equali(argssituations[i]))    
        
print_console("message")
        
        return 
PLUGIN_HANDLED
    
}
    
    switch(
args)
    {
        case 
situations[0]:
        {
            
// code    
        
}
        
        case 
situations[1]:
        {
            
// code    
        
}
    }
    


Last edited by lexzor; 02-12-2021 at 14:18.
lexzor is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 02-12-2021 , 14:22   Re: Need help with optimization
Reply With Quote #7

Quote:
native is "equali"?
A native is any function you're calling. There's no need to call any native when you can just check if the first symbol is valid.

Quote:
I'm using AMXX 1.8.2. Should i download latest version?
I would highly suggest doing that, even though 1.8.2 is still the "official" version.

Quote:
so we can't use switch like this?:
Give me a call when you compile that.
__________________
OciXCrom is offline
Send a message via Skype™ to OciXCrom
kww
Senior Member
Join Date: Feb 2021
Location: Russia
Old 02-12-2021 , 14:28   Re: Need help with optimization
Reply With Quote #8

Quote:
Originally Posted by OciXCrom View Post
A native is any function you're calling. There's no need to call any native when you can just check if the first symbol is valid.
Ok, thanks for explanation
Quote:
Originally Posted by OciXCrom View Post
I would highly suggest doing that, even though 1.8.2 is still the "official" version.
ok i'll do
kww is offline
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 02-12-2021 , 16:18   Re: Need help with optimization
Reply With Quote #9

Everything has already been said, do what OciXCrom suggested you to do, the rest looks fine.

Perhaps if you want to optimize, you could do it somewhere else in your code where it would actually really matter a bit.
__________________
Napoleon_be is offline
Send a message via Skype™ to Napoleon_be
Shadows Adi
AlliedModders Donor
Join Date: Aug 2019
Location: Romania
Old 02-13-2021 , 10:05   Re: Need help with optimization
Reply With Quote #10

You could try this way:
PHP Code:
public xbhop_main(id)
{
    new 
x_arg[33], x_arg2[33]
    
read_argv(1x_argcharsmax(x_arg))
    
read_argv(2x_arg2charsmax(x_arg2))
    
    switch(
x_arg[0]) // Checking the first char from the argument
    
{
        case 
EOS// if(equali(x_arg, "")) this means empty string
        
{
            
client_print(idprint_console"Usage: xbhop3 <help | argument>")
            goto 
_return // jumping to _return label
        
}
        case 
't'
        {
            
toggle_hopper(id)
        }
        case 
'h':
        {
            
xbhop_help(id)
        }
        case 
'a':
        {
            switch(
x_arg2[0]) // Checking the first char in the argument
            
{
                case 
EOS:
                {
                    
client_print(idprint_console"Usage: xbhop3 autojump <1 | 0> (now %i)"autojump)
                    goto 
_return
                
}
                case 
'0':
                {
                    
autojump 0
                
}    
                case 
'1':
                {
                    
autojump 1
                
}
                case default:
                {
                    
client_print(idprint_console"Usage: xbhop3 autojump <1 | 0> (now %i)"autojump)
                    goto 
_return
                
}
            }
            goto 
_return
        
}
        case default:
        {
            
client_print(idprint_console"Usage: xbhop3 <help | argument>")
            goto 
_return
        
}
    }

    
_return// defining the return label
    
return PLUGIN_HANDLED

Edit:
Compiler output for the way above:
PHP Code:
AMX Mod X Compiler 1.8.3-dev+5201
Copyright 
(c1997-2006 ITB CompuPhase
Copyright 
(c2004-2013 AMX Mod X Team

Header size
:            232 bytes
Code size
:              700 bytes
Data size
:              312 bytes
Stack
/heap size:      16384 bytes
Total requirements
:   17628 bytes
Done

Output for your way:
PHP Code:
AMX Mod X Compiler 1.8.3-dev+5201
Copyright 
(c1997-2006 ITB CompuPhase
Copyright 
(c2004-2013 AMX Mod X Team

Header size
:            248 bytes
Code size
:              912 bytes
Data size
:              420 bytes
Stack
/heap size:      16384 bytes
Total requirements
:   17964 bytes
Done

Test plugin:
PHP Code:
/* Sublime AMXX Editor v4.2 */

#include <amxmodx>

new autojump

public toggle_hopper(id)
{
    return 
PLUGIN_HANDLED
}

public 
xbhop_help(id)
{
    return 
PLUGIN_HANDLED
}

public 
xbhop_main(id)
{
    new 
x_arg[33], x_arg2[33]
    
read_argv(1x_argcharsmax(x_arg))
    
read_argv(2x_arg2charsmax(x_arg2))
    
    switch(
x_arg[0])
    {
        case 
EOS:
        {
            
client_print(idprint_console"Usage: xbhop3 <help | argument>")
            goto 
_return
        
}
        case 
't':
        {
            
toggle_hopper(id)
        }
        case 
'h':
        {
            
xbhop_help(id)
        }
        case 
'a':
        {
            switch(
x_arg2[0])
            {
                case 
EOS:
                {
                    
client_print(idprint_console"Usage: xbhop3 autojump <1 | 0> (now %i)"autojump)
                    goto 
_return
                
}
                case 
'0':
                {
                    
autojump 0
                
}    
                case 
'1':
                {
                    
autojump 1
                
}
            }
            goto 
_return
        
}
    }

    
_return:
    return 
PLUGIN_HANDLED

__________________


Accepting Paid Requests, contact PM.

MVP Of The Round View project on GITHUB / AlliedModders
CSGO REMAKE ~ CSGO MOD [STABLE + SOURCE CODE]

Last edited by Shadows Adi; 02-13-2021 at 10:09.
Shadows Adi 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 03:18.


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