4

使用 boost 序列化,我试图找到大对象快速二进制序列化的最佳设置。我的测试表明,对于标记为按位可序列化的结构,我只能在数组(和向量)上获得更好的性能,而不是在单个对象上。

例如,假设我的结构仅由 POD 类型组成

struct BigStruct
{
  double    m1;
  long long m2;
  float     m3;
  // ...
  bool      m499;
  short     m500;
};

namespace boost
{
  namespace serialization
  {
    template <class Archive>
    void serialize(Archive& ioArchive, BigStruct& ioStruct, const unsigned int iVersion)
    {
      ioArchive & ioStruct.m1;
      ioArchive & ioStruct.m2;
      ioArchive & ioStruct.m3;
      // ...
      ioArchive & ioStruct.m499;
      ioArchive & ioStruct.m500;
    }
  }
}

#include <boost/serialization/is_bitwise_serializable.hpp>
BOOST_IS_BITWISE_SERIALIZABLE(BigStruct);

然后,序列化单个对象

{
  std::ofstream outStream(tmpFolder + "/BinarySerializationOfBigStruct.txt", std::ios_base::binary);
  boost::archive::binary_oarchive binOutArchive(outStream, boost::archive::no_header);

  BigStruct bigStruct;

  std::clock_t c_start = std::clock();
  binOutArchive << bigStruct;
  std::clock_t c_end = std::clock();

  // ...compute elapsed time...
}

比序列化 1 个对象的数组花费大约 7 倍的时间

{
  std::ofstream outStream(tmpFolder + "/BinarySerializationOfBigStructArray.txt", std::ios_base::binary);
  boost::archive::binary_oarchive binOutArchive(outStream, boost::archive::no_header);

  BigStruct bigStructArray[1];

  std::clock_t c_start = std::clock();    
  binOutArchive << bigStructArray;
  std::clock_t c_end = std::clock();

  // ...compute elapsed time...
}

我错过了什么吗?这是提升序列化的疏忽吗?

顺便说一句,我正在使用 boost v1.53。

4

0 回答 0