I'm working on a mobile phone application and I see a potential opportunity for performance improvement. Throughout the code I use bool arrays to keep track of which objects are active. For example I'd check to see if the ith MyObject is active by doing the following:
if(activeMyObjects[i]){ // it's active so do something... }
Since the max number of my objects is in the range [5,20] I think I can replace the activeMyObjects bool array with a bitset in the form of a single integer "activeMyObjects". Then I can do the following:
// check if ith object is active
if(activeMyObjects & (1 << i)){ // it's active... }
// activate the ith object
activeMyObjects |= (1 << i);
// reset all myObjects to inactive
activeMyObjects = 0;
// and so on...
Is there anytime when switching to the bitset could actually degrade performance? The language I'm using is c. Also note this code is called very often (frequency in the range 30 - 60 Hz).
Edit: Another piece of information: the bitsets have another advantage in that I can easily tell if any of the objects are active at all. so I can skip a loop where I check each item to see if it's active by first checking if(activeMyObjects). when I use the array this is trickier... my approach was to have an additional counter int which is incremented whenever a MyObject is actived and decremented whenever a MyObject is deactivated... this way I would just check if(activeMyObjectsCount > 0) before the loop where I check each one.