Note that there are some explanatory texts on larger screens.

plurals
  1. POWhen should one be careful using a bitset in place of a bool array?
    primarykey
    data
    text
    <p>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:</p> <pre><code>if(activeMyObjects[i]){ // it's active so do something... } </code></pre> <p>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:</p> <pre><code>// check if ith object is active if(activeMyObjects &amp; (1 &lt;&lt; i)){ // it's active... } // activate the ith object activeMyObjects |= (1 &lt;&lt; i); // reset all myObjects to inactive activeMyObjects = 0; // and so on... </code></pre> <p><strong>Is there anytime when switching to the bitset could actually degrade performance?</strong> The language I'm using is c. Also note this code is called very often (frequency in the range 30 - 60 Hz).</p> <p><strong>Edit:</strong> 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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload