Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fastest c++ serialization?

Tags:

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.

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.

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 ); } 

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 ); } 

About 2.6 seconds for tests = 10000000

like image 683
Jay Avatar asked Sep 03 '10 16:09

Jay


People also ask

How can I speed up serialization?

Speed up serialization To speed up your program, you need to insert a buffer between serializeJson() and WiFiClient . You can do that using the StreamUtils library.

Which type of Serialisation can be used for faster conversion?

c# - Fastest way to serialize and deserialize .

What is serialization C?

Serialization in C# is the process of converting an object into a stream of bytes to store the object to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.


1 Answers

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.

Measure three times, cut once.

like image 96
Hans Passant Avatar answered Sep 23 '22 07:09

Hans Passant