刷新缓冲区是什么意思? [英] What does flushing the buffer mean?

查看:144
本文介绍了刷新缓冲区是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习C ++,我发现了一些我不能理解的东西:

I am learning C++ and I found something that I can't understand:


输出缓冲区可以被显式刷新以强制缓冲区写成
。默认情况下,读取 cin 刷新 cout ; cout 在程序正常结束时也刷新

Output buffers can be explicitly flushed to force the buffer to be written. By default, reading cin flushes cout; cout is also flushed when the program ends normally.

所以刷新缓冲区(例如输出缓冲区):这是否通过删除其中的一切清除缓冲区或通过输出其中的一切清除缓冲区?或者刷新缓冲区意味着完全不同的东西?

So flushing the buffer (for example an output buffer): does this clear the buffer by deleting everything in it or does it clear the buffer by outputting everything in it? Or does flushing the buffer mean something completely different?

推荐答案

考虑写一个文件。这是一个昂贵的操作。如果在你的代码中每次写一个字节,那么每个写一个字节将是非常昂贵的。因此,提高性能的常用方法是将您正在写入的数据存储在临时缓冲区中。只有当有很多数据时才将缓冲区写入文件。通过推迟写入,并一次写入一个大块,性能得到提高。

Consider writing to a file. This is an expensive operation. If in your code you write one byte at a time, then each write of a byte is going to be very costly. So a common way to improve performance is to store the data that you are writing in a temporary buffer. Only when there is a lot of data is the buffer written to the file. By postponing the writes, and writing a large block in one go, performance is improved.

记住,清除缓冲区是从数据传输缓冲区到文件。

With this in mind, flushing the buffer is the act of transferring the data from the buffer to the file.


这是否通过删除缓冲区中的所有内容来清除缓冲区,或者通过输出缓冲区中的所有内容来清除缓冲区? / p>

Does this clear the buffer by deleting everything in it or does it clear the buffer by outputting everything in it?

后者。

这篇关于刷新缓冲区是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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