Raised This Month: $32 Target: $400
 8% 

Bitwise operators memory usage


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Moody92
Veteran Member
Join Date: May 2011
Location: Oman
Old 03-19-2019 , 18:24   Bitwise operators memory usage
Reply With Quote #1

Hello,
A lot of plugins, especially those that are approved use bitwise operators. I realize that using these is primarily to save memory by using a single variable instead of using an array. But how much memory are we talking about here? It's merely bytes that we're sparing here. With modern-day computers and rams, I think memory is not a problem anymore right?

I'd appreciate a little insight on this.

Last edited by Moody92; 03-19-2019 at 19:15.
Moody92 is online now
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 03-19-2019 , 19:21   Re: Bitwise operators memory usage
Reply With Quote #2

Yeah, memory savings and performance benefits would be unnoticeable in a game like HL/cstrike. Though, it's not a bad thing to have a mindset to always code in the most efficient way. I always use bitfields for bools and other things whenever possible.

Bit-wise operators are not only useful for efficiency reasons, they are powerful in what they provide from a functionality perspective. For example, when used as a bool for say 32 players. To check if one is true in an array, you must loop checking each cell in the array, whereas in a bitfield, you can just check that the variable is non-zero.
__________________

Last edited by Bugsy; 03-19-2019 at 19:22.
Bugsy is offline
Moody92
Veteran Member
Join Date: May 2011
Location: Oman
Old 03-19-2019 , 23:08   Re: Bitwise operators memory usage
Reply With Quote #3

Quote:
Originally Posted by Bugsy View Post
Yeah, memory savings and performance benefits would be unnoticeable in a game like HL/cstrike. Though, it's not a bad thing to have a mindset to always code in the most efficient way. I always use bitfields for bools and other things whenever possible.

Bit-wise operators are not only useful for efficiency reasons, they are powerful in what they provide from a functionality perspective. For example, when used as a bool for say 32 players. To check if one is true in an array, you must loop checking each cell in the array, whereas in a bitfield, you can just check that the variable is non-zero.
That's what I figured. I guess you're right that it's good to use them if your intention is to publish the plugin to produce a clean efficient code, but I feel like it's just better to not go that route if your intention is not that.

Thank you.
Moody92 is online now
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 03-19-2019 , 23:15   Re: Bitwise operators memory usage
Reply With Quote #4

I don't think it matters whether or not you plan to publish a plugin, you should always code the best way possible. Once you are comfortable with bit-wise operators and have them in your toolbox, they should be used when you can use them.

The only instance where I would agree to avoid them, is when you are helping someone learn scripting. Bit-wise operators will only confuse them more.
__________________
Bugsy is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 03-20-2019 , 16:20   Re: Bitwise operators memory usage
Reply With Quote #5

Quote:
Originally Posted by Moody92 View Post
I guess you're right that it's good to use them if your intention is to publish the plugin to produce a clean efficient code,
Thank you.
It's not a requirement and it should not be. int is 4 bytes, an array with size of 32 will occupy 128 bytes, that's literally nothing.
If you want to look only at the numbers, then yes, using bitsums is going to occupy less memory and be faster, but do you really care about that?

In practice it will not make any difference and you should not be concerned with stuff like this when writing code, it's only going to distract you from what really matters. If the spacial/temporal complexity of your algorithm it's bad then it will also be bad if you use bitsums, if you declare variables outside a loop, if you use static over new or if you use fakemeta over engine.
What you should really think about is the overall logic of your code and what your algorithm actually does.

For example, think about sorting an array. It matters what technique you use for sorting. If you go with Bogosort(generates all possible permutations of the array and then checks all of them to see which is the sorted one) it's going to take literally ages and no amount of bitwise operations will make it faster. For a small array of 32 elements bogosort will generate 8.6833176 * 10^36
(yes this is 10 raised to power 36) arrays and check all of them to find the sorted one.
On the other hand, if you went with quick sort/merge sort then the algorithm will do at most 160 steps. Now compare 160 to 8 * 10^36.

You should also take into account that bitwise operations are less readable than using a bool array in this case. For most people they are like a black box, meaning most people don't really understand how this operations work and you can't immediately tell what intensive bit manipulating code is doing.

Take a look at this:
PHP Code:
xor y
xor y
xor 
Quick, what this does? You probably have no idea, unless you read about it before and searched for an explanation.

This is what it really does:
PHP Code:
int temp x
y
temp 
No joke, the xor code is swapping two variables. Look at it, it doesn't use a temporary variable and it does bitwise operations, faster and less memory, but at what cost? The xor code is almost impossible to figure out what it does and people will hate you for writing that code.

The main takeaway from this is that you should not throw bit operations everywhere for the sake of "performance", because you are not actually gaining anything, but only making your code harder to maintain and understand for you and for others.
Yes, bit tricks are nice and if you must squeeze every milisecond out of your code then you could use that, but when do you care about every milisecond? Certainly not in a pawn plugin.
To be clear, you can use them if you understand how they work and it's going to be perfectly fine, all I'm saying is that they won't make your code faster/better to a noticeable degree and they are definitely not a must.
Arrays are a basic data structure and processors work very well with contiguous data, they are certainly not the thing that's holding back your code.
__________________

Last edited by HamletEagle; 03-20-2019 at 16:35.
HamletEagle is offline
Moody92
Veteran Member
Join Date: May 2011
Location: Oman
Old 03-21-2019 , 02:11   Re: Bitwise operators memory usage
Reply With Quote #6

Quote:
Originally Posted by HamletEagle View Post
The main takeaway from this is that you should not throw bit operations everywhere for the sake of "performance", because you are not actually gaining anything, but only making your code harder to maintain and understand for you and for others.
Yes, bit tricks are nice and if you must squeeze every milisecond out of your code then you could use that, but when do you care about every milisecond? Certainly not in a pawn plugin.
To be clear, you can use them if you understand how they work and it's going to be perfectly fine, all I'm saying is that they won't make your code faster/better to a noticeable degree and they are definitely not a must.
Arrays are a basic data structure and processors work very well with contiguous data, they are certainly not the thing that's holding back your code.
Yeah, that's the answer I was hoping to get originally. It just seems that bitwise operations are complex, I'd just rather copy an existent macro if it already exists. Though, I'm getting the hang of these operations.

I'm going to use them in smaller hardware for sure though.
Moody92 is online now
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 09:20.


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