gpt4 book ai didi

c++ - 如何用二进制流替换字符串流,以避免强制转换?

转载 作者:行者123 更新时间:2023-11-28 07:41:54 44 4
gpt4 key购买 nike

我目前正在研究几种将复杂的 CGAL 对象实例传输到不同进程的方法。在另一篇文章中,我询问了自定义分配器,但另一种方法是字符串流。 (仅供引用共享内存不是一个选项)

目前的CGAL::Nef_polyhedron_3提供iostreaming:

// In main process
Nef_polyhedron_3 NP3;
stringstream ss;
ss << NP3;
sendToOtherProcess(ss.str());

// In separate process
stringstream ss(stringFromOtherProcess);
Nef_polyhedron_3 NP3;
ss >> NP3;

现在,输出流很慢,输入流慢了四倍。

原因是(我猜)是 Actor 。执行它的类可以在这里找到:https://github.ugent.be/divhaere/cgal/blob/master/include/CGAL/Nef_3/SNC_io_parser.h

仅摘取类(class)成员之一的片段(ln 1481):

  in >> hx >> hy >> hz >> hw;
vh->point() = Point_3(hx,hy,hz,hw);

为数千个点执行此操作需要一段时间。在输出端它更快(不知道为什么)

有什么方法可以提供某种二进制流,它不会进行所有转换?我知道可以使用 read()、write() 写入任何二进制流,但我不能(轻松地)更改 CGAL SNC_io_parser.h。我想理想情况下,我希望能够提供一个流,当被要求流式传输(无论哪种方式)时,它以二进制形式进行。

感谢收到的任何帮助。

马科斯

最佳答案

The reason being (I am guessing) is all the casting... Doing that for many thousands of points is taking a while. On the output side it's faster (not sure why exactly)

您可能希望在分析器下运行它,以便能够确定地判断时间花在了哪里。

Is there any way I can provide some kind of binary stream, that won't do all the casting?

您正在使用 std::ostreamoperator<<()std::istreamoperator>>()将算术类型序列化为文本和从文本序列化的接口(interface),此行为无法更改。

您可以复制 Nef_polyhedron_3 的序列化函数并让它们接受一些其他序列化为二进制格式或从二进制格式序列化的流类型。

关于c++ - 如何用二进制流替换字符串流,以避免强制转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15682704/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com