缓冲区冲洗:“\\\<br/>” vs. std :: endl [英] Buffer flushing: &quot;\n&quot; vs. std::endl

查看:176
本文介绍了缓冲区冲洗:“\\\<br/>” vs. std :: endl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

C ++:&std:std :: endl” vs“ \ n”

加速C ++ ,提到两个问题:

In Accelerated C++, two things are mentioned:


  1. 大多数系统需要大量时间才能将字符写入输出设备。因此,C ++累积要写入缓冲区的字符,并等待缓冲区被刷新。

  1. Most systems take a significant amount of time to write characters to an output device. Because of this, C++ accumulates characters to be written in a buffer and waits for the buffer to be flushed.

缓冲区可以刷新的一种方法是,通过使用 std :: endl 明确告诉它这样做。

One way a buffer can be flushed is if we explicitly tell it to do so by using std::endl.

这让我很奇怪:显而易见的好处是非常小和不明显的除了最大的输出,但使用\\\
比使用 std :: endl 更快,或者\\\
也刷新缓冲区? / p>

This made me wonder: Obviously the benefits would be very small and unnoticeable for everything except the largest of outputs, but is using "\n" faster than using std::endl, or does "\n" also flush a buffer?

推荐答案

使用'\\\
'不刷新缓冲区,确实比使用std :: endl更快。

Using '\n' does not flush the buffer and is indeed faster than using std::endl.

在典型的I / O中,输出在写入目标器件之前进行缓冲。这样,当写入缓慢访问设备(如文件)时,它不必在每个单个字符后访问设备。将缓冲区刷新冲洗到设备上,导致一定的性能开销。

In typical I/O, output is buffered before it's written to the intended device. This way, when writing to slow to access devices(like files), it doesn't have to access the device after every single character. Flushing "flushes" the buffer onto the device, causing a definite performance overhead.

- 适应自: C ++ - endl并刷新缓冲区

这篇关于缓冲区冲洗:“\\\<br/>” vs. std :: endl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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