Sign in to follow this  

Create bitset subset of larger bitset.

Recommended Posts

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[s] = bitset1[s];

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<240> bitset2(bitset1); that doesn't compile.

Thanks in advance.

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this