Enabling buffering means that you’re not directly interfacing with the OS’s
representation of a file, or its file system API. Instead, a chunk of data is
read from the raw OS filestream into a buffer until it is consumed, at which
point more data is fetched into the buffer. In terms of the objects you get,
you’ll get a
BufferedIObaseobject wrapping an underlying
RawIObase(which
represents the raw file stream).
这有什么好处?与原始流进行良好的接口可能会导致较高的延迟,因为操作系统必须摆弄诸如硬盘之类的物理对象,并且在所有情况下这都是不可接受的。假设您想每5毫秒从文件中读取三个字母,并且文件位于硬壳旧硬盘甚至网络文件系统上。与其尝试每隔5毫秒从原始文件流中读取数据,不如将文件中的一堆字节加载到内存中的缓冲区中,然后随意使用它。
选择什么大小的缓冲区将取决于您如何使用数据。对于上面的示例,缓冲区大小为1个字符非常糟糕,三个字符是可以的,并且3个字符的较大倍数不会对用户造成明显的延迟将是理想的。



