I have a scenario where I need to store the state of 64 entities (max).
I could do this with either a bool array or a 64bit variable (where I use 1 bit to represent each entity).
Clearly bool would be faster because it is a build in system for handling which only requires a single check. The second option is far ore efficient though as it only takes up 8 bytes... whereas bool if i remember correctly uses a whole byte meaning you save 56 bytes.
Neo wrote:I was wondering which would be more efficient/faster.
Any ideas on this?
I'd be tempted to use a 64-bit unsigned int and inline assembly (defined as a macro) based on the "BT" instruction.
Cheers,
Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Is this C99? Or C++? (Because you use bool, which doesn't exist in C89.) Userspace, I assume - or kernel work?
For userspace C++ (or if your kernel-space library includes it), you could take a look at <bitset>... depending on what you want to do with those flags, that might be more conventient / efficient.
Don't use vector<bool>, though. It's broken. (Because operator[] doesn't return a pointer to an element, it isn't really a vector, or a STL container at that, so the whole abstraction is broken. You might want deque<bool>, but <bitset> is optimized for bool.)
Every good solution is obvious once you've found it.
Neo wrote:
I was wondering which would be more efficient/faster.
Any ideas on this?
You have to deside on which is more applicable to the problem. Here's some factors that you need to consider:
The array is faster then the 64-bit UINT, if you are using it for single bool manipulation. but is slower when using it for multiple bool manipulation.
Using the array procedures more readable code then the 64-bit UINT.
the integer is a lot harder to upgrade.
The choice is yours
Microsoft: "let everyone run after us. We'll just INNOV~1"