Imagine there’s a fixed and constant set of ‘options’ (e.g. skills). Every object (e.g. human) can either have or not have any of the options.
Should I maintain a member list-of-options for every object and fill it with options?
OR:
Is it more efficient (faster) to use a bitarray where each bit represents the respective option’s taken (or not taken) status?
-edited:-
To be more specific, the list of skills is a vector of strings (option names), definitely shorter than 256.
The target is for the program to be AS FAST as possible (no memory concerns).
That rather depends. If the number of options is small, then use several
boolmembers to represent them. If the list grows large, then both your options become viable:enumto symbolically represent the options) takes a constant, and very small, amount of space, and getting a certain option takes O(1) time;std::setorunordered_setof them, might be more space-efficient, but only if the number of options is huge, and it is expected that a very small number of them will be set per object.When in doubt, use either a bunch of
boolmembers, or abitset. Only if profiling shows that storing options becomes a burden, consider a dynamic list or set representation (and even then, you might want to reconsider your design).Edit: with less than 256 options, a
bitsetwould take at most 64 bytes, which will definitely beat any list or set representation in terms of memory and likely speed. A bunch ofbools, or even an array ofunsigned char, might still be faster because accessing a byte is commonly faster than accessing a bit. But copying the structure will be slower, so try several options and measure the result. YMMV.