AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [question] array of booleans or integer?(optimization) (https://forums.alliedmods.net/showthread.php?t=140706)

ARES[ro] 10-15-2010 18:52

[question] array of booleans or integer?(optimization)
 
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?

Bugsy 10-15-2010 19:15

Re: [question] array of booleans or integer?(optimization)
 
Use a single variable and leave it un-tagged. If you need further help post or pm your problem code.

Bugsy 10-15-2010 19:34

Re: [question] array of booleans or integer?(optimization)
 
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



ARES[ro] 10-15-2010 19:46

Re: [question] array of booleans or integer?(optimization)
 
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

fysiks 10-15-2010 19:48

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

Originally Posted by ARES[ro] (Post 1325861)
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.

Bugsy 10-15-2010 19:53

Re: [question] array of booleans or integer?(optimization)
 
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.

ARES[ro] 10-15-2010 20:00

Re: [question] array of booleans or integer?(optimization)
 
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!

Bugsy 10-15-2010 20:04

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

Originally Posted by ARES[ro] (Post 1325869)
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.

ARES[ro] 10-15-2010 20:20

Re: [question] array of booleans or integer?(optimization)
 
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.


All times are GMT -4. The time now is 10:24.

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