Quote:
Originally Posted by Moody92
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:
x = x xor y
y = x xor y
x = x xor y
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
x = y
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.
__________________