Create bitset subset of larger bitset.

Started by
1 comment, last by stu2000 13 years, 5 months ago
Anyone know how to optimize this code much better?
std::bitset<320> bitset1;
//bunch of code here that alters bitset 1 values


//create a subset of bitset 1
std::bitset<240> bitset2;
for(unsigned int s=0; s < 240; s++)
{
bitset2 = bitset1;<br>}<br><br><br>The values 320 and 240 are not specific, just used as an example. Seems a horrible waste of cpu to loop through a bitset 240 times to copy a section of it. Unfortunately if you try and cast it with td::bitset&lt;240&gt; bitset2(bitset1); that doesn't compile.<br><br>Thanks in advance.<br>Stu
Advertisement
As far as I can tell you have three options here.

1. Hope the compiler doesn't generate code that is too slow for that loop in release. It's possible that unrolling it manually 8 times would help there, but I suspect that still wouldn't make it anywhere near as fast as the obvious memcpy() implementation.

2. Make all your bitsets the same size.

3. Implement your own bitset template, with a fast copying function.
Thanks, yah I think I am going to have to write own bitset template in order to make them dynamic sizeable.

This topic is closed to new replies.

Advertisement