简体   繁体   English

我如何用二进制流代替字符串流,以避免转换?

[英]How can I substitute a binary stream for a string stream, to avoid casting?

I'm currently investigating a few methods to transfer a complex CGAL object instance to a different process. 我目前正在研究将复杂的CGAL对象实例传输到其他进程的几种方法。 In another post, I've asked about custom allocators, but another method is string streams. 在另一篇文章中,我询问了自定义分配器,但是另一种方法是字符串流。 (FYI shared memory is not an option) (FYI共享内存不是一种选择)

The current CGAL::Nef_polyhedron_3 provides iostreaming: 当前的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;

Now, the output streaming is slow, and the input streaming is four times slower. 现在,输出流变慢,而输入流变慢了四倍。

The reason being (I am guessing) is all the casting. 原因是(我猜)是全部演员。 The class which does it can be found here: https://github.ugent.be/divhaere/cgal/blob/master/include/CGAL/Nef_3/SNC_io_parser.h 可以在这里找到实现它的类: https : //github.ugent.be/divhaere/cgal/blob/master/include/CGAL/Nef_3/SNC_io_parser.h

To take just a snippet of one of the members of the class (ln 1481): 仅摘录班级成员之一的话(1481年):

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

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? 有什么办法可以提供某种二进制流,而不能完成所有转换? I'm aware one can write to any stream in binary with read(), write(), but I can't (easily) change the CGAL SNC_io_parser.h. 我知道一个人可以使用read(),write()以二进制形式写入任何流,但我不能(轻松地)更改CGAL SNC_io_parser.h。 I guess ideally I'd like to be able to provide a stream that when asked to stream (either way), it did it in binary. 我想理想情况下,我希望能够提供一种流,当被要求以两种方式流式传输时(以任何一种方式)。

Any help gratefully received. 非常感谢任何帮助。

Marcos 马可斯

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) 在输出端,它更快(不确定为什么会这样)

You may like to run it under profiler to be able to tell with certainty where the time is being spend. 您可能希望在事件探查器下运行它,以便能够确定何时花在哪里。

Is there any way I can provide some kind of binary stream, that won't do all the casting? 有什么办法可以提供某种二进制流,而不能完成所有转换?

You are using std::ostream with operator<<() and std::istream with operator>>() interfaces which serialize arithmetic types into and from text and this behaviour can not be changed. 您正在使用std::ostreamoperator<<()std::istreamoperator>>()该序列运算类型和从文本和这种行为不能被改变的接口。

You can replicate the serialization functions of Nef_polyhedron_3 and make them accept some other stream types that serialize into/from binary formats. 您可以复制Nef_polyhedron_3的序列化函数,并使它们接受一些其他类型的序列化为二进制格式的流。

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

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