c++ - 理想的缓冲区大小是多少?

标签 c++ c io

Possible Duplicate:
How do you determine the ideal buffer size when using FileInputStream?

使用 C++ 的 istream 系列的 read() 或 C 的 fread() 从文件(或任何输入流)读取原始数据时>,必须提供一个缓冲区,以及要读取多少数据。我见过的大多数程序似乎都是在 512 和 4096 之间任意选择 2 的幂。

  1. 是否有理由必须/应该是 2 的幂,或者这只是程序员对 2 的幂的自然倾向?
  2. “理想”的数字是多少? “理想”是指它是最快的。我认为它必须是底层设备缓冲区大小的倍数?或者可能是底层流对象的缓冲区?无论如何,我将如何确定这些缓冲区的大小?一旦我这样做了,使用它的倍数是否会比仅使用确切的大小来提高速度?

编辑
大多数答案似乎是在编译时无法确定。我可以在运行时找到它。

最佳答案

来源:
How do you determine the ideal buffer size when using FileInputStream?

Optimum buffer size is related to a number of things: file system block size, CPU cache size and cache latency.

Most file systems are configured to use block sizes of 4096 or 8192. In theory, if you configure your buffer size so you are reading a few bytes more than the disk block, the operations with the file system can be extremely inefficient (i.e. if you configured your buffer to read 4100 bytes at a time, each read would require 2 block reads by the file system). If the blocks are already in cache, then you wind up paying the price of RAM -> L3/L2 cache latency. If you are unlucky and the blocks are not in cache yet, the you pay the price of the disk->RAM latency as well.

This is why you see most buffers sized as a power of 2, and generally larger than (or equal to) the disk block size. This means that one of your stream reads could result in multiple disk block reads - but those reads will always use a full block - no wasted reads.

确保这一点通常还会导致影响读取和后续处理的其他性能友好参数:数据总线宽度对齐、DMA 对齐、内存高速缓存行对齐、虚拟内存页面的整数。

关于c++ - 理想的缓冲区大小是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10698339/

相关文章:

c - 程序卡在while循环中

java - ProcessBuilder 与 Runtime.getRuntime().exec 不同的行为

java - TeeInputStream 和 PipedStream 在任何情况下都不起作用

go - 从 io.Writer 写的内容中读取内容

c++ - 代码中未调用的函数在运行时被调用

c++ - 实例化结构时出现段错误 - C++

c++ - 在公共(public)方法中使用 pop_back(),但是一旦我退出该方法, vector 就会返回到原始方法

c++ - 在 C++ 中追加不同的字段

c - 固定数组的大小而不是在 C 中使用指针

c - 单词切碎/混合程序