what is the best way to concatenate 2 bitsets?
For example i\'ve got
boost::dynamic_bitset<> test1( std::string(\"1111\") );
boost::dynamic_bit
I ran a test comparing the following two approaches:
/* ... */
for( int ii = 0; ii < 1000000; ++ii ) {
std::bitset<16> v1( randomUlongs[ii] );
std::bitset<16> v2( randomUlongs[ii+1] );
#ifdef STRING_TEST
std::bitset<32> v3( v1.to_string() + v2.to_string() );
#else
std::bitset<32> v3( v2.to_ulong() | (v1.to_ulong() << 16) );
/* print out v3 */
}
... where randomUlongs
was constant during each run (a large array in a header) to avoid anything contaminating results. I timed it with:
~ time for ((ii=0; ii<1000; ii++)); do ./bitset_test >/dev/null; done
Under Linux (x86_i686) with gcc 4.4.6
at optimization level 3: the string concatenation was fastest, by a factor of 2.
Under Solaris (sparc) with gcc 3.4.3
and Sun Studio C++ 5.12 (2011/11/16)
, both with optimization level 3: the non-string approach was fastest by a factor of 10.
I think you'll find the "fastest" solution is highly dependent on the compiler, though I suppose platform could play a significant role as well.