简体   繁体   English

C ++中序列化对象的性能

[英]Performance of Serialized Objects in C++

I'm wondering if there is a fast way to dump an STL set to disk and then read it back later. 我想知道是否有一种快速的方法可以将STL set转储到磁盘上,然后稍后再读回。

The internal structure of a set is a binary tree, so if I serialize it naively, when I read it back the program will have to go though the process of inserting each element again. set的内部结构是一棵二叉树,因此,如果我天真地序列化它,那么当我读回它时,程序将不得不经过再次插入每个元素的过程。 I think this is slow even if it is read back in correct order, correct me if I am wrong. 我认为即使按正确的顺序读取它也很慢,如果我写错了,请纠正我。

Is there a way to "dump" the memory containing the set into disk and then read it back later? 有没有一种方法可以将包含该集合的内存“转储”到磁盘中,然后稍后再读回去? That is, keep everything in binary format, thus avoiding the re-insertion. 也就是说,将所有内容保持为二进制格式,从而避免了重新插入。

Do the boost serialization tools do this? boost序列化工具会这样做吗?

Thanks! 谢谢!

EDIT: oh I should probably read, http://www.parashift.com/c++-faq-lite/serialization.html I will read it now... no it doesn't really help 编辑:哦,我应该读一下, http://www.parashift.com/c++-faq-lite/serialization.html我现在将阅读它……没有真正的帮助

No, and if you are actually reading it back from a hard disk (or probably any permanent storage), the mechanical part will be the bottleneck. 不,如果您实际上是从硬盘(或可能是任何永久性存储)中读取它,则机械部分将成为瓶颈。

If you put the container in a contiguous block of memory, that block must have some free space, and reading that wasted space from the disk wastes time… and disk space. 如果将容器放在连续的内存块中,则该块必须有一些可用空间,并且从磁盘读取浪费的空间会浪费时间和磁盘空间。

This is classic premature optimization. 这是经典的过早优化。

If you really find yourself needing it, Boost Interprocess has (relatively) serialization-friendly containers. 如果您确实需要它, Boost Interprocess具有(相对)易于序列化的容器。

As each set element is somewhere on the heap, you cannot just dump the structure to disk. 由于每个set元素都在堆上的某个位置,因此您不能仅将结构转储到磁盘上。 You therefore need a proper serialization routine that goes through each element. 因此,您需要一个遍历每个元素的适当的序列化例程。

To read the element back again, you can use "hints", which allow you to hint to the insert method where the element should be inserted. 要再次读回元素,可以使用“提示”,它允许您提示应该在其中插入元素的insert方法。 This can bring the construction of the set back to linear complexity instead of n log n. 这可以使集合的构造回到线性复杂度,而不是n log n。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM