[Include] mBits - better than Booleans
#1

Name: mBits
Version: 1.2
Author: Maku

Description:
Small include for beginners. It saves memory by using binary numbers.
Boolean, which occupy memory as much as an ordinary variable. Boolean can be replaced by binary numbers, which allows you to save 31 true / false on one variable. In short, what gives us the use of this include?
Instead of this:
Код:
new bool:PlayerAdmin[MAX_PLAYERS];
new bool:Nitro[MAX_PLAYERS];
new bool:Mute[MAX_PLAYERS];
new bool:InFun[MAX_PLAYERS];
new bool:VIP[MAX_PLAYERS];
We have this:
Код:
new playerBit[MAX_PLAYERS];
Functions:

1. Create:
Код:
new variable;
2. Check value of bit. Return: true or false
Код:
IsBit(var, bit)
3. Switch value of bit (if we had true, then we have a false and vice versa). Return: always "1"
Код:
SwitchBit(var, bit)
4. Set value of bit (true)
Код:
SetBitTrue(var, bit)
5. Set value of bit (false)
Код:
SetBitFalse(var, bit)
Defines:
Код:
#define BIT_VAR_1 (1)
#define BIT_VAR_2 (2)
#define BIT_VAR_3 (4)
#define BIT_VAR_4 (8)
#define BIT_VAR_5 (16)
#define BIT_VAR_6 (32)
#define BIT_VAR_7 (64)
#define BIT_VAR_8 (128)
#define BIT_VAR_9 (256)
#define BIT_VAR_10 (512)
#define BIT_VAR_11 (1024)
#define BIT_VAR_12 (2048)
#define BIT_VAR_13 (4096)
#define BIT_VAR_14 (8192)
#define BIT_VAR_15 (16384)
#define BIT_VAR_16 (32768)
#define BIT_VAR_17 (65536)
#define BIT_VAR_18 (131072)
#define BIT_VAR_19 (262144)
#define BIT_VAR_20 (524288)
#define BIT_VAR_21 (1048576)
#define BIT_VAR_22 (2097152)
#define BIT_VAR_23 (4194304)
#define BIT_VAR_24 (8388608)
#define BIT_VAR_25 (16777216)
#define BIT_VAR_26 (33554432)
#define BIT_VAR_27 (67108864)
#define BIT_VAR_28 (134217728)
#define BIT_VAR_29 (268435456)
#define BIT_VAR_30 (536870912)
#define BIT_VAR_31 (1073741824)
Informations:

1. Parameter "bit" is the simplest words "slot save." So like a "new bool:Info[100];" = 100 slots. This parameter should be one of the definitions above (unless someone knows the power of two to 31)

2. Use the "bit" values other than the definitions of "BIT_VAR_x" will lead to errors in reading the value or overwriting.

3. Created a variable for the bit values can also be treated as an ordinary integer and saving it to a MySQL database or files, making it easy to read and save the player settings.

4. Include You can also replace with the macro-definitions gaining maximum optimization, but for beginners I recommend stocks what I created.

5. Limit the stored values for each variable is 31, because one variable is 32 bits (32 bit shift the stored number to a negative).

Download:
v1.2 - http://pastebin.com/VutW630D
Test script - http://pastebin.com/MjK1LgEj

Thanks for Yahas.

Код:
BitFunctions: 1707ms
mBits: 475ms
rBits: 6926ms
Booleans: 524ms
  Filterscript 'fastbits.amx' loaded.
reloadfs fastbits
  Filterscript 'fastbits.amx' unloaded.
BitFunctions: 1762ms
mBits: 475ms
rBits: 7106ms
Booleans: 532ms
  Filterscript 'fastbits.amx' loaded.
mBits is faster than BitFunctions by Abyss Morgan and rBits by RyDeR`. Compared to booleans is almost identical (small difference and depends on the situation. Once faster, once slower (max 200ms difference at 1milion execution)). mBits uses less memory than booleans (32 times less).
Reply
#2

Is it really worth saving few kilobytes of memory at the cost of performance? You are making use of stocks for altering the bits of the variable instead of using defines.

I guess setting a bit using this include would be around at least 10x slower than simply setting a variable.

You should update your include to use defines instead of stocks.
Reply
#3

Fine and dandy but why would anyone need to bother using this? A bit of saved memory is frivolous compared to having to having to change syntax for useless savings.
Reply
#4

These things are useful for really large data structures. For example, a 2-d array size [MAX_PLAYERS][3000] not only use 12MB of memory, but it also adds more than 1 second to the compile time.
If you had used this, Pottus, your gamemode wouldn't take 90 seconds to compile!
If that array is a boolean array, the size could be [MAX_PLAYERS][94].
Reply
#5

Quote:

Is it really worth saving few kilobytes of memory at the cost of performance?

PAWN scripts should have few kilobytes. SAMP gamemodes very straining PAWN. But yes, stocks = extra calls and timings.
Tomorrow I will add an alternative version with defines.

Maybe in the future i will make more functions like few integers in one var (eg. usage for pickups IDs - array size 2000 instead of 4000)
Reply
#6

Nice work, but is it really worth the amount you're saving? I mean, it seems a lot easier for beginners to read a code like this:

Код:
new bool:bool;

if(!bool)
than something like this

Код:
new mybit;

if(IsBit(mybit, 0))
Reply
#7

Hi,

So first off this is some pretty advanced stuff - rep+ for commanding a mastery of it MakuPL! I think you could probably improve on it in areas - specifically, you could eliminate your defines by bitshifting 1 left by setid (1 << setid) in your methods.

I agree with Pottus on the optimization front however. There are very few occasions where I personally would sacrifice CPU cycles for memory - so few that I can't even think of a scenario off the top of my head. Single threaded as SA-MP is; slow operations directly affect the playability of the server.

Memory, besides aesthetically being a large number (that is still a fraction of a fraction of a modern server's total RAM), is pretty much sidelined.

By its very nature this is many times slower than just directly setting or un-setting a variable - least of all because it has to set and return a new variable anyway.

As always I'd be happy to be proven wrong though.
Reply
#8

How does this differ from rBits (https://sampforum.blast.hk/showthread.php?tid=275142) ?
Reply
#9

rBits has manipulations group of bits. +1
BitFunction is simpler and faster.

https://sampforum.blast.hk/showthread.php?tid=591223
Reply
#10

Quote:
Originally Posted by TakeiT
Посмотреть сообщение
I mean, it seems a lot easier for beginners to read a code like this:

Код:
new bool:bool;

if(!bool)
than something like this

Код:
new mybit;

if(IsBit(mybit, 0))
Use of binary data storage variable, by using only one bit is meaningless. So you create one variable anyway.
For example this include is good when you save such a boolean values in the database.

Quote:
Originally Posted by Alcatrik
Посмотреть сообщение
you could eliminate your defines by bitshifting 1 left by setid (1 << setid) in your methods.
Slower code compilation vs execution. This solution adds further calculations for the processor during execution.

Quote:
Originally Posted by Macluawn
Посмотреть сообщение
How does this differ from rBits (https://sampforum.blast.hk/showthread.php?tid=275142) ?
I didnt know that include, but when I read theard I can say that the difference is simplicity for my include and functionality for rBits.

Quote:
Originally Posted by AbyssMorgan
Посмотреть сообщение
BitFunction is simpler and faster.

https://sampforum.blast.hk/showthread.php?tid=591223
Please show me the code and results.


Update to version 1.1
- removed "Bit:" flags
- stocks rewrited to defines
- SetBit is now SetBitTrue and SetBitFalse
- parameters is now "var" and "bit" (old = "bits" and "setid");
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)