schnellste c ++ Serialisierung?
Guten Morgen alle
Ich suche nach einer sehr schnellen binären Serialisierungstechnik für c ++. Ich muss nur die in Objekten enthaltenen Daten serialisieren (keine Zeiger usw.). Ich möchte, dass es so schnell wie möglich geht. Wenn es spezifisch für x86-Hardware ist, ist das akzeptabel.
Ich bin mit den C-Methoden dafür vertraut. Als Test habe ich eine Reihe von Techniken getestet. Ich habe festgestellt, dass die C-Methode 40% schneller ist als die beste C ++ - Methode, die ich implementiert habe.
Vorschläge zur Verbesserung der C ++ - Methode (oder von Bibliotheken, die dies tun)? Gibt es etwas Gutes für Dateien mit Speicherzuordnung?
Vielen Dan
// 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 );
}
Ungefähr 1,6 Sekunden für 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 );
}
Ungefähr 2,6 Sekunden für Tests = 10000000