0

我有一个项目要从 C++ 转换为 Python。该项目最初是由其他人编写的。逻辑描述如下:

“它加载了 20 个纯文本文件,其中每行代表 7 列的记录。然后解析每一行并将所有数据加载到 C++ 向量和映射的对象中。然后使用二进制序列化输出 C++ 向量和映射到相应的 20 个二进制文件”。

我不知道作者使用二进制序列化的目的是什么,但我想可能是由于三个原因:

  1. 与纯文本文件相比,减少了二进制文件的大小
  2. 减少初始化时间,因为系统通过将这些文件加载​​到内存并填充结构来启动。
  3. 除了初始化阶段之外,它可能会在运行时加速,尽管可能性很小。

我对对象序列化的经验很少,当我现在用python重写它时,我不知道我是否应该使用对象序列化。在 Python 中,对象序列化是显着加快程序速度,还是减少了存储在磁盘上的文件大小?还是有什么其他好处?我知道缺点是它使程序更复杂。

在 Python 中实现上述逻辑时,我是否也应该使用对象序列化?

4

1 回答 1

1

Sounds like it's almost certainly the case that the point is to avoid this part: "It then parses each line and loaded all data into objects of C++ vectors and maps".

If the binary machine data in the "C++ vectors and maps" can later be loaded directly, then all the expenses of parsing the text to recreate them from scratch can be skipped.

Can't answer whether you "should" do the same, though, without knowing details of the expenses involved. Not enough info here to guess.

于 2021-12-09T21:46:15.657 回答