增加缓冲区cout [英] increase buffer for cout

查看:303
本文介绍了增加缓冲区cout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

引用 setbuf()是否影响cout?

我想增加缓冲区大小以提高cout的性能(它是
,通常重定向到磁盘)

I want to increase the buffer size to improve the performance of cout (it is usually redirected to disk)

我可以:

std::cout.rdbuf()->pubsetbuf(some_buffer, buffer_size);

以及

ios::sync_with_stdio(false);

这是否有意义?

:我也使用多线程,所以我希望减少同步的需要。

Also I am using multiple threads, so I was hoping to reduce the need for synchronization.

推荐答案

我将首先检查刷新次数,这将使任何更大的缓冲区大小不相关。

I would first check on the number of flushes that will make any larger buffer size irrelevant.

特别注意,如果你有很多cout<< endl在你的代码中,并尝试用cout<< '\\\
',如果你不需要endl的flushing效果。

Especially look, if you have a lot of cout << endl in your code and try replacing them by cout << '\n' instead, if you do not need the flushing effect of endl.

作为最后一种手段,在尝试优化寻找根本原因之前,尝试strace或类似工具来查看实际发生的系统调用的数量。 (我希望这有助于你的问题)。

As a last resort, before you try "optimizing" look for the root cause, e.g. try strace or similar tool to see the number of system calls actually occuring. (I hope this is helpful to your problem).

只有,如果这一切已经被处理,一个更大的缓冲区大小实际上可以帮助减少系统调用的次数。

Only, if that all is already looked after, a larger buffer size can actually help to reduce the number of system calls.

cout输出的另一个减慢是,它经常准备与多个线程同步输出,即使你只使用一个线程。这也可以大大减慢I / O,因为大的缓冲区没有用的开销。

Another slowing of cout output is, that it is often prepared to synchronize output with multiple threads, even when you only use one thread. This again can slow I/O heavily because of the overhead where a larger buffer is of no use.

这篇关于增加缓冲区cout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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