自动选择文件 I/O 的缓冲区大小

8
我有一个非常基本的疑问。经常需要编写使用缓冲文件I/O的应用程序,每次都面临选择缓冲区大小的困境,最终往往只能通过试错的方式解决,结果很不理想。我想知道是否有任何方法或算法可以根据底层平台自动确定最佳缓冲区大小,就像Teracopy在处理Windows中的文件时所做的那样。我主要使用Qt进行GUI开发。
如果可能的话,可以提供C/C++/C#/Java的小例子!
谢谢!
2个回答

15

在Java中,通常最佳块大小约为L1缓存大小,通常为32 KB。在Java中,至少选择1024字节或1 MB并没有太大区别(<20%)。

如果您按顺序读取数据,通常操作系统足够智能,可以检测到这一点,并为您预取数据。

您可以执行以下操作。此测试似乎显示了使用的块大小之间的显着差异。

public static void main(String... args) throws IOException {
    for (int i = 512; i <= 2 * 1024 * 1024; i *= 2)
        readWrite(i);
}

private static void readWrite(int blockSize) throws IOException {
    ByteBuffer bb = ByteBuffer.allocateDirect(blockSize);
    long start = System.nanoTime();
    FileChannel out = new FileOutputStream("deleteme.dat").getChannel();
    for (int i = 0; i < (1024 << 20); i += blockSize) {
        bb.clear();
        while (bb.remaining() > 0)
            if (out.write(bb) < 1) throw new AssertionError();
    }
    out.close();
    long mid = System.nanoTime();
    FileChannel in = new FileInputStream("deleteme.dat").getChannel();
    for (int i = 0; i < (1024 << 20); i += blockSize) {
        bb.clear();
        while (bb.remaining() > 0)
            if (in.read(bb) < 1) throw new AssertionError();
    }
    in.close();
    long end = System.nanoTime();
    System.out.printf("With %.1f KB block size write speed %.1f MB/s, read speed %.1f MB/s%n",
            blockSize / 1024.0, 1024 * 1e9 / (mid - start), 1024 * 1e9 / (end - mid));
}

打印

With 0.5 KB block size write speed 96.6 MB/s, read speed 169.7 MB/s
With 1.0 KB block size write speed 154.2 MB/s, read speed 312.2 MB/s
With 2.0 KB block size write speed 201.5 MB/s, read speed 438.7 MB/s
With 4.0 KB block size write speed 288.0 MB/s, read speed 733.9 MB/s
With 8.0 KB block size write speed 318.4 MB/s, read speed 711.8 MB/s
With 16.0 KB block size write speed 540.6 MB/s, read speed 1263.7 MB/s
With 32.0 KB block size write speed 726.0 MB/s, read speed 1370.9 MB/s
With 64.0 KB block size write speed 801.8 MB/s, read speed 1536.5 MB/s
With 128.0 KB block size write speed 857.5 MB/s, read speed 1539.6 MB/s
With 256.0 KB block size write speed 794.0 MB/s, read speed 1781.0 MB/s
With 512.0 KB block size write speed 676.2 MB/s, read speed 1221.4 MB/s
With 1024.0 KB block size write speed 886.3 MB/s, read speed 1501.5 MB/s
With 2048.0 KB block size write speed 784.7 MB/s, read speed 1544.9 MB/s

这个测试没有显示的是硬盘仅支持60 MB/s的读取和40 MB/s的写入。你所测试的只是缓存中的读写速度。如果这是你唯一关心的,那么你应该使用内存映射文件。

int blockSize = 32 * 1024;
ByteBuffer bb = ByteBuffer.allocateDirect(blockSize);
FileChannel out = new FileOutputStream("deleteme.dat").getChannel();
for (int i = 0; i < (1024 << 20); i += blockSize) {
    bb.clear();
    while (bb.remaining() > 0)
        if (out.write(bb) < 1) throw new AssertionError();
}
out.close();

long start = System.nanoTime();
FileChannel in = new FileInputStream("deleteme.dat").getChannel();
MappedByteBuffer map = in.map(FileChannel.MapMode.READ_ONLY, 0, in.size());
in.close();
long end = System.nanoTime();
System.out.printf("Mapped file at a rate of %.1f MB/s%n",
        1024 * 1e9 / (end - start));

打印

Mapped file at a rate of 589885.5 MB/s

之所以如此快速,因为它直接将操作系统磁盘缓存中的数据映射到应用程序的内存中(因此无需复制)。


纯粹的惊人!!! 这在Java中几乎就像神一样……但我不知道我能在C/C++上实现到什么程度,因为我的大多数应用程序都是本地的,并且需要尽可能快。还有一件事:您的方法是否适用于在写回数据之前操作数据的应用程序(加密应用程序)?? 顺便说一句,Math.pow("谢谢!!!!", (10 / 0)); - Rahul De
加密应用程序,像大多数处理数据的应用程序一样,很可能会受到CPU限制。在这种情况下,缓冲区大小不太重要,因为CPU成本非常高。你的缓存大小可能更有影响。个人认为,Java中可以做的任何事情都可以在C或C++中实现。 - Peter Lawrey

1

我在C语言中看到了这段代码:

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>

int main()
{
  struct stat fi;
  stat("/", &fi);
  printf("%d\n", fi.st_blksize);
  return 0;
}

它返回最佳块大小。您需要使用它来完成这个任务。我使用流源到目的地,使用16 * 块大小以获得最佳性能。因为这个测试将在空闲的PC上进行,并带有一些硬件/操作系统,所以可以揭示最佳结果。但这并不是真实情况。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接