缓冲 I/O 如何减少使用无缓冲 I/O 时会发生的开销?

How does buffered I/O reduce the overhead which would occur if unbuffered I/O was used?

来自 this tutorial

Most of the examples we've seen so far use unbuffered I/O. This means each read or write request is handled directly by the underlying OS. This can make a program much less efficient, since each such request often triggers disk access, network activity, or some other operation that is relatively expensive.

To reduce this kind of overhead, the Java platform implements buffered I/O streams. Buffered input streams read data from a memory area known as a buffer; the native input API is called only when the buffer is empty. Similarly, buffered output streams write data to a buffer, and the native output API is called only when the buffer is full.

我了解磁盘访问、网络 activity 等操作会导致底层 OS 的内存或执行时间开销。

但问题是程序如何reading-from/writing-to某个内存区域(缓冲区)减少这个开销?

我认为这是添加了几个额外的步骤:首先,程序请求 OS,例如,从文件中读取数据并将其写入缓冲区,然后程序从中读取数据缓冲区。

您可能知道,磁盘驱动器、网络连接或其他设备的 IO 操作比内存访问慢得多。通过在内存中缓冲IO操作,软件可以减少对IO设备执行的操作次数。