AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Optimization (https://forums.alliedmods.net/showthread.php?t=293126)

EFFx 01-23-2017 15:28

Optimization
 
Guys, I've seen to much times here scripters talking about optimizations for good read/visual/style on the plugin. I'm learning more about it reading tutorials and plugins on that forum. I've searched a little bit and I want know more about it.

Ok, so my question is:

If I have so much get_user_flags() or any other native.

if I make something like this

PHP Code:

haveTheFlag(index,id 0)
    return 
bool:(get_user_flags(index) & id

And use instead of all get_user_flags() on the plugin

PHP Code:

if(haveTheFlag(id) & ADMIN_KICK

or

PHP Code:

#define haveTheFlag(%1,%2) (get_user_flags(%1) & %2) 

That can be considered an optimization? What happen with the compiler?

edon1337 01-23-2017 15:35

Re: Optimization
 
Stick to the easiest way.
PHP Code:

get_user_flags 

No private function/ stock / define.

Black Rose 01-23-2017 16:18

Re: Optimization
 
Quote:

Originally Posted by EFFx (Post 2489273)
PHP Code:

haveTheFlag(index,id 0)
    return 
bool:(get_user_flags(index) & id


I may be wrong here but I think this creates some kind of internal call to a stock function which will in some way eat more resources. However I do not think this is reason for avoidance. If it makes things easier and cleaner it's the right way to go. But I don't think one native call is enough to create a stock.

Quote:

Originally Posted by EFFx (Post 2489273)
PHP Code:

#define haveTheFlag(%1,%2) (get_user_flags(%1) & %2) 


This is basically copy-paste. The compiler will find all instances of "haveTheFlag" and just replace them with whatever is to the right of it. It will have no effect what so ever on the performance compared to writing it as usual since it is exactly the same.

I did som efficiency tests using 5 different methods.
Spoiler: None of them are sticking out as bad, they are all fine.
Spoiler

EFFx 01-23-2017 16:28

Re: Optimization
 
So, basically, all these formats you've used doesn't make any big differente to the compiler, i'm right? The best way is use the original native instead of making new methods like macro, stock, array?

Ah, about this

PHP Code:

if(get_user_flags(id) & ADMIN_KICK)
{
     
client_print(id,print_chat,"[AMXX]: You have not enough money to buy this!")
}
else if(
get_user_flags(id) & ADMIN_SLAY)
{
     
client_print(id,print_chat,"[AMXX]: You have not enough money to buy this! Write /admin for buy admin privilegies")
}
else
{
     
client_print(id,print_chat,"[AMXX]: You have not enough money to buy this! Write /vip for buy vip privilegies")


This should be

PHP Code:

switch(get_user_flags(id))
{
      case 
ADMIN_KICKclient_print(id,print_chat,"[AMXX]: You have not enough money to buy this!")
      case 
ADMIN_SLAYclient_print(id,print_chat,"[AMXX]: You have not enough money to buy this! Write /admin for buy admin privilegies")
      default: 
client_print(id,print_chat,"[AMXX]: You have not enough money to buy this! Write /vip for buy vip privilegies")


Or a stock, because I have that format so much times, like 15-20 I think.

PHP Code:

stock SendUserAdv(id)
{
      switch(
get_user_flags(id))
      {
            case 
ADMIN_KICKclient_print(id,print_chat,"[AMXX]: You have not enough money to buy this!")
            case 
ADMIN_SLAYclient_print(id,print_chat,"[AMXX]: You have not enough money to buy this! Write /admin for buy admin       privilegies")
            default: 
client_print(id,print_chat,"[AMXX]: You have not enough money to buy this! Write /vip for buy vip privilegies")
      }



Edit: off-topic LUL, now I understand your signature, haha gj

Black Rose 01-23-2017 16:35

Re: Optimization
 
Unfortunately you can't use a switch because get_user_flags() will return a bitsum. So it will never be EQUAL TO ADMIN_KICK, it will INCLUDE ADMIN_KICK.
You could use this:
Code:
switch ( get_user_flags(id) & ( ADMIN_KICK | ADMIN_SLAY ) ) {     case ADMIN_KICK : {} // Only kick     case ADMIN_SLAY : {} // Only slay     case ( ADMIN_KICK | ADMIN_SLAY ) : {} // Both     default : {} // Neither }
But I'm sure you can see that it's just not good enough. You immediately have to create one more option. And for every option added, the number of options around grows exponentially since you have to take consideration for all the different combinations of options.
Also, if you are considering converting it to cvars to be able to change the different levels required more easily by the end user, switches are out completely.

If you have all of these in multiple locations I would say that your menu system is not dynamic enough. You can definitely make it more dynamic and end up with one handler with just one message.
But if you find it hard to create that kind of advanced menu, then I would suggest a stock.

In the end it's a function that is called whenever someone uses a command or a menu which is rare in general. You do not have to worry about performance at all and should use the method that makes the code easy to work with without breaking readability.

EFFx 01-23-2017 16:43

Re: Optimization
 
I know about it, thats want I want, check if user has ADMIN_KICK included on all his access. Not just one flag,
Example: abcdefghijklmnopqrstu, he have T flag included, so its considered VIP

I'm curious now, what "hard menu" are you talking about?

I want optimize it because I think I will post this here, you know. And optimization is the most important thing on plugins you know. But I understand you side and I'll continue with the current format. If someone told me to change, i'll do it.

Depresie 01-23-2017 16:47

Re: Optimization
 
That is no optimization on what you did on the first post, the main goal of the optimization is to use less resources, the secondary being the readability

What you did in the first post is not helping when it comes to using less resources neither helping when it comes to readability

An example of optimization is this
Instead of calling a native twice, you call it once and store it's return value, then you compare it with your desired values
Spoiler


Macros / Define are for readability, not improving performance, you usually make them to save your ass writing a long story ( set_pev(id, (pev_) asadsad bla bla blabla )_)

Stocks have basically the same purpose, instead of writing 10 times the same thing in every function you need it, you just write it in one, and call that stock

EFFx 01-23-2017 16:50

Re: Optimization
 
@Depresie,

Yea, thats the one thing of all I have. I don't have only one check like this, I have so much, as I said, 15-20 I think. So, should I always store the get_user_flag() in one variable on all checks or should I make a stock with that feature?

Depresie 01-23-2017 16:56

Re: Optimization
 
Yeah, if you have to do the exact same things in more than one function, just make a stock/function for that it's easier to handle

EFFx 01-23-2017 17:11

Re: Optimization
 
Just one thing,

If I have two things that uses the get_user_flags() but their features are different. Like this first one I said and another one. Should I create a global variable, store on client_putinserver() and client_infochanged() and use that variable on all functions that check user's flag?


All times are GMT -4. The time now is 20:50.

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