Want more features on Pastebin? Sign Up, it's FREE!
Guest

Concatenate Boost::Dynamic_Bitset - Performance

By: a guest on Jun 23rd, 2010  |  syntax: C++  |  size: 2.44 KB  |  views: 424  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. #include <iostream>
  3. #include <boost/dynamic_bitset.hpp>
  4. #include <boost/date_time/posix_time/posix_time.hpp>
  5. using namespace boost::posix_time;
  6.  
  7.  
  8. const int CYCLES = 10000;
  9.  
  10.  
  11. boost::dynamic_bitset<> concatenateBitsets(const boost::dynamic_bitset<>& first, const boost::dynamic_bitset<>& second)
  12. {
  13.     boost::dynamic_bitset<> firstCopy(first);
  14.     boost::dynamic_bitset<> secondCopy(second);
  15.  
  16.     //Increase the size of the bit buffer to fit the data being placed in it
  17.     firstCopy.resize(first.size() + second.size());
  18.     secondCopy.resize(first.size() + second.size());
  19.  
  20.     //shift the bits in the firstCopy to the left
  21.     firstCopy <<= second.size();
  22.  
  23.     //"copy" the bits from the secondCopy into the firstCopy
  24.     firstCopy |= secondCopy;
  25.     return firstCopy;
  26. }
  27.    
  28.  
  29. int main()
  30. {
  31.     //init the time-counting objects
  32.     ptime firstValue = ptime(microsec_clock::local_time());
  33.     ptime secondValue = ptime(microsec_clock::local_time());
  34.     time_duration diff = secondValue - firstValue;
  35.    
  36.     //init the bitsets
  37.     boost::dynamic_bitset<> intBitset(32, 0xffffaaaa);
  38.     boost::dynamic_bitset<> boolBitset(1, true);
  39.     boost::dynamic_bitset<> charBitset(8, 'x');
  40.  
  41.  
  42.    
  43.     //Test the String-Append method
  44.     firstValue = ptime(microsec_clock::local_time());
  45.  
  46.     std::ostringstream bitsetConcat;
  47.     bitsetConcat << intBitset; //initial fill
  48.     for(int index = 0; index < CYCLES; index++)
  49.     {
  50.         bitsetConcat << intBitset;
  51.         bitsetConcat << boolBitset;
  52.         bitsetConcat << charBitset;
  53.     }
  54.     boost::dynamic_bitset<> bitsetConcatenated( bitsetConcat.str() );
  55.  
  56.     secondValue = ptime(microsec_clock::local_time());
  57.     diff = secondValue - firstValue;
  58.     std::cout << "Stringmethod lasts " << diff.fractional_seconds() << " msec" << std::endl;
  59.  
  60.  
  61.    
  62.     //Test the Bitshift method
  63.     firstValue = ptime(microsec_clock::local_time());
  64.  
  65.     boost::dynamic_bitset<> bitsetConcat2(intBitset); //initial fill
  66.     for(int index = 0; index < CYCLES; index++)
  67.     {
  68.         bitsetConcat2 = concatenateBitsets(bitsetConcat2, intBitset);
  69.         bitsetConcat2 = concatenateBitsets(bitsetConcat2, boolBitset);
  70.         bitsetConcat2 = concatenateBitsets(bitsetConcat2, charBitset);
  71.     }
  72.     secondValue = ptime(microsec_clock::local_time());
  73.     diff = secondValue - firstValue;
  74.     std::cout << "Shiftmethod lasts " << diff.fractional_seconds() << " msec" << std::endl;
  75.    
  76.     return 0;
  77. }
clone this paste RAW Paste Data