#include <iostream>
#include <boost/dynamic_bitset.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace boost::posix_time;
const int CYCLES = 10000;
boost::dynamic_bitset<> concatenateBitsets(const boost::dynamic_bitset<>& first, const boost::dynamic_bitset<>& second)
{
boost::dynamic_bitset<> firstCopy(first);
boost::dynamic_bitset<> secondCopy(second);
//Increase the size of the bit buffer to fit the data being placed in it
firstCopy.resize(first.size() + second.size());
secondCopy.resize(first.size() + second.size());
//shift the bits in the firstCopy to the left
firstCopy <<= second.size();
//"copy" the bits from the secondCopy into the firstCopy
firstCopy |= secondCopy;
return firstCopy;
}
int main()
{
//init the time-counting objects
ptime firstValue = ptime(microsec_clock::local_time());
ptime secondValue = ptime(microsec_clock::local_time());
time_duration diff = secondValue - firstValue;
//init the bitsets
boost::dynamic_bitset<> intBitset(32, 0xffffaaaa);
boost::dynamic_bitset<> boolBitset(1, true);
boost::dynamic_bitset<> charBitset(8, 'x');
//Test the String-Append method
firstValue = ptime(microsec_clock::local_time());
std::ostringstream bitsetConcat;
bitsetConcat << intBitset; //initial fill
for(int index = 0; index < CYCLES; index++)
{
bitsetConcat << intBitset;
bitsetConcat << boolBitset;
bitsetConcat << charBitset;
}
boost::dynamic_bitset<> bitsetConcatenated( bitsetConcat.str() );
secondValue = ptime(microsec_clock::local_time());
diff = secondValue - firstValue;
std::cout << "Stringmethod lasts " << diff.fractional_seconds() << " msec" << std::endl;
//Test the Bitshift method
firstValue = ptime(microsec_clock::local_time());
boost::dynamic_bitset<> bitsetConcat2(intBitset); //initial fill
for(int index = 0; index < CYCLES; index++)
{
bitsetConcat2 = concatenateBitsets(bitsetConcat2, intBitset);
bitsetConcat2 = concatenateBitsets(bitsetConcat2, boolBitset);
bitsetConcat2 = concatenateBitsets(bitsetConcat2, charBitset);
}
secondValue = ptime(microsec_clock::local_time());
diff = secondValue - firstValue;
std::cout << "Shiftmethod lasts " << diff.fractional_seconds() << " msec" << std::endl;
return 0;
}