与多个流相同的字符串 [英] Same string to multiple streams

查看:175
本文介绍了与多个流相同的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须向多个流发送相同的字符串(例如日志消息)。

以下哪个解决方案是最有效的?

I have to send the same string (e.g. a log message) to multiple streams.
Which of the following solutions is the most efficient?


  1. 为每个流重建相同的字符串,并将其发送到流本身。

  1. Rebuild the same string for each stream and send it to the stream itself.

outstr1 << "abc" << 123 << 1.23 << "def" << endl;  
outstr2 << "abc" << 123 << 1.23 << "def" << endl;  
outstr3 << "abc" << 123 << 1.23 << "def" << endl;  


  • 使用字符串运算符构建字符串一次,并将其发送到所有流。 p>

  • Build the string once with string's operators, and send it to all the streams.

    std::string str = "abc" + std::to_string(123) + std::to_string(1.23) + "def";  
    outstr1 << str;  
    outstr2 << str;  
    outstr3 << str;  
    


  • 使用流构建字符串一次,并将其发送到所有流: p>

  • Build the string once with a stream, and send it to all the streams:

    std::stringstream sstm;  
    sstm << "abc" << 123 << 1.23 << "def" << endl;  
    std::string str = sstm.str();  
    outstr1 << str;  
    outstr2 << str;  
    outstr3 << str;  
    


  • 部分或全部输出流在RAM磁盘上。

    Some or all of these output streams could be on a RAM disk.

    任何其他方法可以做同样的事情吗?

    Any other ways to do the same thing?

    推荐答案

    正确的方式来做这样的事情是有一个流缓冲区写入到多个目的地,并通过 std :: ostream 使用此流缓冲区。这样的代码看起来好像只写一次,但字符被发送多次。搜索teebuf Dietmar将在同一主题上找到几个变体。

    The "proper" way to do something like this is to have a stream buffer writing to multiple destinations and use this stream buffer via a std::ostream. This way the code looks as if it writing just once but the characters are sent multiple times. Searching for "teebuf Dietmar" will find a few variations on the same theme.

    还要评论你的问题:三种选择中哪一种是最快的取决于您拥有的确切表达式:

    To also comment on your question: Which one of the three alternatives is the fastest depends on the exact expressions you have:


    1. 需要评估所涉及的表达式并执行三次转换。

    2. 实际上创建并销毁多个流,并对 std :: string 。我希望这是最慢的。

    3. 仍然创建一个流(实际上应该是 std :: ostringstream ),并分配一些内存。

    1. needs to evaluate the involved expressions and perform the conversions three times. Depending on what you actually do this may still be fairly fast.
    2. actually creates and destroys multiple streams and does multiple allocations for std::string. I'd expect this to be slowest.
    3. still creates a stream (which should actually be a std::ostringstream) and allocates some memory. Out of your options I'd expect it to be fastest.

    使用 teebuf 可能是最快的,至少,当它做一些缓冲,但只使用固定的suze数组,缓冲区和流缓冲区指针的数组。注意,你需要重写 sync()来及时处理缓冲区。

    Using a teebuf is probably fastest, at least, when it does some buffering but uses only fixed suze arrays, both for the buffer and the array of stream buffer pointers. Note, that you'll need to override sync() to deal with the buffer in a timely manner, though.

    要确定实际的效果,您需要测量!

    To determine the actual performance you'll need to measure!

    这篇关于与多个流相同的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆