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

[question] array of booleans or integer?(optimization)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
ARES[ro]
Senior Member
Join Date: Apr 2010
Old 10-15-2010 , 18:52   [question] array of booleans or integer?(optimization)
Reply With Quote #1

In my plugin i made my own variable wich stores certain flags:
for example
isAlive (int) and i do bitwise OR shifted bits (id) to set isAlive, id
the compiler gives me a tag mismatch warning every time i use if(!getBit( isAlive, id ))
so my question is shud i use an integer or bool:isAlive[33] to avoid the tag mismatch warnings.
So my question is shud i use an integer or an array of booleans?
__________________
okay

Last edited by ARES[ro]; 10-15-2010 at 19:12.
ARES[ro] is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 10-15-2010 , 19:15   Re: [question] array of booleans or integer?(optimization)
Reply With Quote #2

Use a single variable and leave it un-tagged. If you need further help post or pm your problem code.
__________________

Last edited by Bugsy; 10-15-2010 at 19:29.
Bugsy is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 10-15-2010 , 19:34   Re: [question] array of booleans or integer?(optimization)
Reply With Quote #3

Here's a organized way of storing flags

You can have up to 32 flags.
PHP Code:
enum ( <<=)
{
     
IsAlive 1,
     
IsAdmin,
     
HasGodmode,
     
HasSpeed,
}

#define SetFlag(%1,%2)   (g_PlayerFlags[%1] |= %2)
#define RemoveFlag(%1,%2)   (g_PlayerFlags[%1] &= ~(%2))
#define CheckFlag(%1,%2)   (g_PlayerFlags[%1] & %2)

new g_PlayerFlags33 ]; 

SetFlagid IsAdmin );

RemoveFlagid IsAdmin );

if ( 
CheckFlagid IsAdmin ) )
{
    
//Code

__________________
Bugsy is offline
ARES[ro]
Senior Member
Join Date: Apr 2010
Old 10-15-2010 , 19:46   Re: [question] array of booleans or integer?(optimization)
Reply With Quote #4

Well yeah i have macros too ^^
PHP Code:
#define getBit(%1,%2)    (%1 & 1 << (%2 & 31)) 
and love the way u put it more thanks
__________________
okay
ARES[ro] is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 10-15-2010 , 19:48   Re: [question] array of booleans or integer?(optimization)
Reply With Quote #5

Quote:
Originally Posted by ARES[ro] View Post
Well yeah i have macros too ^^
PHP Code:
#define getBit(%1,%2)    (%1 & 1 << (%2 & 31)) 
and love the way u put it more thanks
I think where yours is wrong is the that it should be "%2 % 31". Bugsy, correct me if I'm wrong.
__________________
fysiks is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 10-15-2010 , 19:53   Re: [question] array of booleans or integer?(optimization)
Reply With Quote #6

IMO, it would be easier to manage and code using what I posted above where each player their own bitfield of options versus having a variable of each flag and setting a player id bit to 1/0.

@fysiks, there was a discussion somewhere regarding the speed of id-1, id % 31, and id & 31, id & 31 turned out to be the most efficient.
__________________

Last edited by Bugsy; 10-15-2010 at 19:57.
Bugsy is offline
ARES[ro]
Senior Member
Join Date: Apr 2010
Old 10-15-2010 , 20:00   Re: [question] array of booleans or integer?(optimization)
Reply With Quote #7

So its faster? I only counted out the server itself. Im glad to hear.
Also i do know about assembly code and such i just dont look up each operator im using lol!
__________________
okay
ARES[ro] is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 10-15-2010 , 20:04   Re: [question] array of booleans or integer?(optimization)
Reply With Quote #8

Quote:
Originally Posted by ARES[ro] View Post
So its faster? I only counted out the server itself. Im glad to hear.
Also i do know about assembly code and such i just dont look up each operator im using lol!
Yes, X & 31 is more efficient than X % 31 or X-1. Not that you would notice a difference using either but it's always best to use the most efficient methods.
__________________
Bugsy is offline
ARES[ro]
Senior Member
Join Date: Apr 2010
Old 10-15-2010 , 20:20   Re: [question] array of booleans or integer?(optimization)
Reply With Quote #9

Well youre right in assembly commands it does make a HUGE difference. I did learn in college that % uses a division and some shifts and AND wil save you a lot of cpu with square numbers.
__________________
okay

Last edited by ARES[ro]; 10-15-2010 at 20:24.
ARES[ro] 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 05:04.


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