最快的c ++序列化? [英] fastest c++ serialization?

查看:128
本文介绍了最快的c ++序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,

我正在为c ++寻找一种非常快速的二进制序列化技术。我只需要序列化包含在对象中的数据(没有指针等)。我希望它和
一样快。如果它特定于x86硬件是可以接受的。

I'm searching for a very fast binary serialization technique for c++. I only need to serialize data contained in objects (no pointers etc.). I'd like it to be as fast as possible. If it's specific to x86 hardware that's acceptable.

我熟悉这样做的C方法。作为一个测试,我有标准的几个技术。我发现C方法比我实现的最好的C ++方法快40%。

I'm familiar with the C methods of doing this. As a test I've bench marked a couple of techniques. I've found the C method is 40% faster than the best C++ method I implemented.

有关如何改进C ++方法的任何建议(或者这样做的库) ?
任何适用于内存映射文件的内容?

Any suggestions on how to improve the C++ method (or libraries that do this)? Anything good available for memory mapped files?

感谢

// c style writes
{
   #pragma pack(1)
   struct item
   {
      uint64_t off;
      uint32_t size;
   } data;
   #pragma pack

   clock_t start = clock();

   FILE* fd = fopen( "test.c.dat", "wb" );
   for ( long i = 0; i < tests; i++ )
   {
      data.off = i;
      data.size = i & 0xFFFF;
      fwrite( (char*) &data, sizeof(data), 1, fd );
   }
   fclose( fd );

   clock_t stop = clock();

   double d = ((double)(stop-start))/ CLOCKS_PER_SEC;
   printf( "%8.3f seconds\n", d );
}

测试的大约1.6秒= 10000000

About 1.6 seconds for tests = 10000000

// c++ style ofstream writes

// define a DTO class
class test
{
public:
   test(){}

   uint64_t off;
   uint32_t size;

   friend std::ostream& operator<<( std::ostream& stream, const test& v );
};

// write to the stream
std::ostream& operator<<( std::ostream &stream,  const test& v )
{
   stream.write( (char*)&v.off, sizeof(v.off) );
   stream.write( (char*)&v.size, sizeof(v.size) );
   return stream;
}

{
   test data;

   clock_t start = clock();

   std::ofstream out;
   out.open( "test.cpp.dat", std::ios::out | std::ios::trunc | std::ios::binary );
   for ( long i = 0; i < tests; i++ )
   {
      data.off = i;
      data.size = i & 0xFFFF;
      out << data;
   }
   out.close();

   clock_t stop = clock();

   double d = ((double)(stop-start))/ CLOCKS_PER_SEC;
   printf( "%8.3f seconds\n", d );
}

测试时约2.6秒= 10000000

About 2.6 seconds for tests = 10000000

推荐答案

只有很少很少的现实生活中的情况。你只有序列化,使你的对象与某种外部资源兼容。磁盘,网络等。在资源上传输序列化数据的代码总是比序列化对象所需的代码慢几个数量级。如果你使序列化代码的速度快两倍,那么你的整体操作速度不会超过0.5%。这不是风险也不是努力。

There are just very few real-life cases where that matters at all. You only ever serialize to make your objects compatible with some kind of external resource. Disk, network, etcetera. The code that transmits the serialized data on the resource is always orders of magnitude slower then the code needed to serialize the object. If you make the serialization code twice as fast, you've made the overall operation no more than 0.5% faster, give or take. That is worth neither the risk nor the effort.

测量三次,切一次。

这篇关于最快的c ++序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆