簡體   English   中英

自動選擇文件I / O的緩沖區大小

[英]Automatically selecting buffer size for File I/O

我有一個非常基本的懷疑。 通常,我必須編寫使用緩沖文件I / O的應用程序,並且每當我面臨選擇緩沖區大小的困境時,我最終會通過非常討厭的結果進行反復試驗。 我想知道是否有任何方法或算法可以根據Teracopy在Windows中處理文件時所基於的底層平台自動確定作業的最佳緩沖區大小。 我主要使用Qt作為GUI。

如果可能的話,非常感謝C / C ++ / C#/ Java中的一個小例子!

謝謝!

在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

這是如此之快,因為它只是將OS磁盤緩存中的數據直接映射到應用程序的內存中(因此不需要復制)

我在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 *塊大小的目標,以獲得最佳性能。 因為這個測試將通過一些硬件/操作系統在空閑時顯示出最佳效果。 但不是真實的情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM