tags:

views:

105

answers:

5

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.

+1  A: 

Nope. At the end of the day, it's a simple integer shift, which is less costly than an array access by all accounts. Just be wary that converting the bitset to a tertiary set (perhaps active, inactive, and undefined), or attempting to add data to each entry is now out of the question without refactoring.

BrainCore
Also, if you ever have over 32 objects, you'll need to expand your bitset to be an int array. In which case, you'll still be accessing array entries, but it'll conserve space.
BrainCore
+2  A: 

You won't be gaining any performance on most platforms. What you'll save on is memory footprint.

jfclavette
+2  A: 

Depending on how often the bitset is allocated, you can easily end up occupying more memory with the bitset version.

The code to access a particular bit is much larger than the code to access a full byte of memory - if there's only going to be a handful of these arrays, but it's accessed often, you'll get a smaller footprint leaving it like it is.

Anon.
the bitsets are allocated once on the stack at the beginning of the program. they are accessed extremely frequenty. the operations on the bitset are: set the ith bit, check if the ith bit is set, unset the ith bit, unset all bits. given this info would you advise using the arrays or the bitsets?
MrDatabase
You've really just got to test it yourself. I know it sucks to sink dev time into something of an uncertain value, but this is extremely implementation dependent. It really could go either way. It depends upon the compiler optimization, the native chipset commands, the caching structure, and the relative speeds of CPU / caches / data. I'd really be surprised if someone here could authoritatively answer this for you, particularly since you didn't list any of those variables.
Matt B.
If they're allocated once and accessed often, it's going to use less memory to stick with the array. Loading that one boolean is then a single instruction - while with a bitset, you're looking at three (load, shift, mask). Performance-wise you'd need to benchmark each option on your target hardware in order to get an idea of the results.
Anon.
+1  A: 

If speed is more crucial than memory footprint (since this is mobile app overall) then you can store masks in an array and access them as activeMyObjects & mask[i].

If speed is critical, then you'd be better off with named constants rather than an array of masks.
Anon.
+1  A: 

I'm told that some PowerPC chips (don't know if it's all of them or not - see this question for example) are slow at doing variable-length bitshifts as you're doing with your (1 << i) instructions. So on those chips it seems quite likely that the bitset would run considerably slower than the bool array.

That might well not be a problem for you, but it's an interesting little case :)

Peter