Raised This Month: $ Target: $400
 0% 

Optimization


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
EFFx
Veteran Member
Join Date: Feb 2016
Location: São Paulo, Brasil
Old 01-23-2017 , 15:28   Optimization
Reply With Quote #1

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?
__________________
• Ranking System • AutoMix 5vs5 System
• Web Ban System • Plugins for free

____________________________________________
For private works:
• Discord: EFFEXo#8850 • Steam: EFFEXo

Last edited by EFFx; 01-23-2017 at 15:37.
EFFx is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 01-23-2017 , 15:35   Re: Optimization
Reply With Quote #2

Stick to the easiest way.
PHP Code:
get_user_flags 
No private function/ stock / define.
__________________
edon1337 is offline
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 01-23-2017 , 16:18   Re: Optimization
Reply With Quote #3

Quote:
Originally Posted by EFFx View Post
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 View Post
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
__________________

Last edited by Black Rose; 01-23-2017 at 16:31.
Black Rose is offline
EFFx
Veteran Member
Join Date: Feb 2016
Location: São Paulo, Brasil
Old 01-23-2017 , 16:28   Re: Optimization
Reply With Quote #4

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
__________________
• Ranking System • AutoMix 5vs5 System
• Web Ban System • Plugins for free

____________________________________________
For private works:
• Discord: EFFEXo#8850 • Steam: EFFEXo

Last edited by EFFx; 01-23-2017 at 16:30.
EFFx is offline
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 01-23-2017 , 16:35   Re: Optimization
Reply With Quote #5

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.
__________________

Last edited by Black Rose; 01-23-2017 at 16:43.
Black Rose is offline
EFFx
Veteran Member
Join Date: Feb 2016
Location: São Paulo, Brasil
Old 01-23-2017 , 16:43   Re: Optimization
Reply With Quote #6

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.
__________________
• Ranking System • AutoMix 5vs5 System
• Web Ban System • Plugins for free

____________________________________________
For private works:
• Discord: EFFEXo#8850 • Steam: EFFEXo

Last edited by EFFx; 01-23-2017 at 16:53.
EFFx is offline
Depresie
Veteran Member
Join Date: Nov 2013
Old 01-23-2017 , 16:47   Re: Optimization
Reply With Quote #7

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
__________________

Last edited by Depresie; 01-23-2017 at 16:51.
Depresie is offline
EFFx
Veteran Member
Join Date: Feb 2016
Location: São Paulo, Brasil
Old 01-23-2017 , 16:50   Re: Optimization
Reply With Quote #8

@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?
__________________
• Ranking System • AutoMix 5vs5 System
• Web Ban System • Plugins for free

____________________________________________
For private works:
• Discord: EFFEXo#8850 • Steam: EFFEXo

Last edited by EFFx; 01-23-2017 at 16:55.
EFFx is offline
Depresie
Veteran Member
Join Date: Nov 2013
Old 01-23-2017 , 16:56   Re: Optimization
Reply With Quote #9

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
__________________

Last edited by Depresie; 01-23-2017 at 16:57.
Depresie is offline
EFFx
Veteran Member
Join Date: Feb 2016
Location: São Paulo, Brasil
Old 01-23-2017 , 17:11   Re: Optimization
Reply With Quote #10

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?
__________________
• Ranking System • AutoMix 5vs5 System
• Web Ban System • Plugins for free

____________________________________________
For private works:
• Discord: EFFEXo#8850 • Steam: EFFEXo

Last edited by EFFx; 01-23-2017 at 17:12.
EFFx 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 20:50.


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