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

Function checks vs variables


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
hornet
AMX Mod X Plugin Approver
Join Date: Mar 2010
Location: Australia
Old 07-22-2011 , 09:30   Function checks vs variables
Reply With Quote #1

I've seen in many larges mods / plugins, where the person who wrote the code used a bool variable to determine things such as if the user is alive or connected. Is it really that much more efficient for me to use my own variable ... if( g_bIsAlive[ id ] ) as oppose to if( is_user_alive( id ) ) ??

Thanks.
hornet is offline
NiHiLaNTh
Way Past Expiration
Join Date: May 2009
Location: Latvia
Old 07-22-2011 , 09:55   Re: Function checks vs variables
Reply With Quote #2

Personally, I think that it depends on the size of the plugin.If you in your plugin are using 'big' natives(like FM_AddToFullPack, FM_Player[Pre][post]Think, FM_CmdStart, FM_UpdateClientData, FM_StartFrame etc.) or functions that are called a lot of times, than you should cache alive/connected status for every single player.
P.S. I think you should read this thread for more info.
__________________

NiHiLaNTh is offline
Send a message via Skype™ to NiHiLaNTh
abdul-rehman
Veteran Member
Join Date: Jan 2010
Location: Khi, Pakistan
Old 07-22-2011 , 10:13   Re: Function checks vs variables
Reply With Quote #3

Yes its very usefull if your plugin is big
Read This Tutorial
__________________

My Plugins For ZP

Inactive due to College and Studies
abdul-rehman is offline
Send a message via Yahoo to abdul-rehman Send a message via Skype™ to abdul-rehman
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 07-22-2011 , 12:59   Re: Function checks vs variables
Reply With Quote #4

Typically the less native calls you have the better. For caching bools it's best to use bit fields for efficiency. You can cache any value as long as you update the cached value when it changes.

ie.
Alive - Update value when spawned/dies
Team - Update if changes team
Connected - Update when connects/disconnects
__________________

Last edited by Bugsy; 07-22-2011 at 16:04.
Bugsy is offline
bibu
Veteran Member
Join Date: Sep 2010
Old 07-22-2011 , 13:33   Re: Function checks vs variables
Reply With Quote #5

Quote:
Originally Posted by Bugsy View Post
I usually do not cache is alive or is connected since these change often...
What? They just change on connect, and spawn/death. Do you believe for example an "is_user_alive" check in prethink is better than hooking spawn/death and setting the bit fields for it?
__________________
Selling tons of my own private works.
Accepting paid work for clans and communities.
Don't hesitate to contact me.
bibu is offline
SnoW
Veteran Member
Join Date: Oct 2008
Location: Finland WisdomNuggets: 8
Old 07-22-2011 , 13:50   Re: Function checks vs variables
Reply With Quote #6

Please, do not use the expression of "'big' native" when talking about forwards. Even the size of the plugin can correlate a bit with the need of caching, this is a barely acceptable advise. In general it is better for performance to cache values assuming that the native for checking the value is called more often than the forward changing the value. Somehow caching is not always necessary as it is more writing but still a good habit when using for example already mentioned per frame called forwards.

Quote:
Originally Posted by Bugsy View Post
I usually do not cache is alive or is connected since these change often and are inexpensive on the CPU to call.
A player does not die or connect often, in fact these are one of the most infrequently happening events on the whole server. Everything happens more ( some equally ) often expect a couple of exceptions like a map change.

Quote:
Originally Posted by Bugsy View Post
For caching bools it's best to use bit fields for efficiency.
You are not talking about memory only but also CPU and you are wrong. The defines you use for bit fields are slower for checking a single value than using an array. It is faster only in the rare case of checking if any of the values is true or that if all are false.
SnoW is offline
Send a message via MSN to SnoW
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 07-22-2011 , 15:46   Re: Function checks vs variables
Reply With Quote #7

I don't bother responding to your posts anymore Snow, you are usually only trying to instigate a pissing contest.

bibu: I agree, I retract what I said about alive/connected checks.
__________________

Last edited by Bugsy; 07-22-2011 at 15:50.
Bugsy 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 04:06.


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