Javaで大きなファイルを一行ずつ読み書きをする最速の方法

限られたメモリ(約64MB)の中で、Javaで大きなファイル(0.5~1GB)を読み書きする最速の方法をいろいろと探しています。ファイルの各行はレコードを表すので、私はそれらを行ごとに取得する必要があります。ファイルは通常のテキストファイルです。

BufferedReaderやBufferedWriterを試しましたが、どうもベストな選択とは思えません。サイズ0.5GBのファイルを、何も処理せずに読み書きのみで、約35秒かかっています。読み込みだけでも10秒程度かかるので、ここでのボトルネックは書き込みだと思います。

バイトの配列を読み込もうとしましたが、そうすると読み込んだ各配列の行を検索するのに時間がかかります。

何かご提案があればお願いします。 ありがとうございます。

ソリューション

あなたの本当の問題は、ハードウェアが限られていて、ソフトウェアで何をやってもあまり変わらないことなのではないでしょうか。メモリやCPUに余裕があれば、より高度なトリックが有効ですが、ファイルがキャッシュされていないためにハードディスクで待機しているだけなら、あまり大きな違いはありません。

ちなみに、10秒で500MB、50MB/secは、HDDの一般的な読み込み速度です。

どの時点でシステムが効率的にファイルをキャッシュできなくなるかを確認するために、以下を実行してみてください。

public static void main(String... args) throws IOException {
    for (int mb : new int[]{50, 100, 250, 500, 1000, 2000})
        testFileSize(mb);
}

private static void testFileSize(int mb) throws IOException {
    File file = File.createTempFile("test", ".txt");
    file.deleteOnExit();
    char[] chars = new char[1024];
    Arrays.fill(chars, 'A');
    String longLine = new String(chars);
    long start1 = System.nanoTime();
    PrintWriter pw = new PrintWriter(new FileWriter(file));
    for (int i = 0; i < mb * 1024; i++)
        pw.println(longLine);
    pw.close();
    long time1 = System.nanoTime() - start1;
    System.out.printf("Took %.3f seconds to write to a %d MB, file rate: %.1f MB/s%n",
            time1 / 1e9, file.length() >> 20, file.length() * 1000.0 / time1);

    long start2 = System.nanoTime();
    BufferedReader br = new BufferedReader(new FileReader(file));
    for (String line; (line = br.readLine()) != null; ) {
    }
    br.close();
    long time2 = System.nanoTime() - start2;
    System.out.printf("Took %.3f seconds to read to a %d MB file, rate: %.1f MB/s%n",
            time2 / 1e9, file.length() >> 20, file.length() * 1000.0 / time2);
    file.delete();
}

メモリがたくさんあるLinuxマシンで

Took 0.395 seconds to write to a 50 MB, file rate: 133.0 MB/s
Took 0.375 seconds to read to a 50 MB file, rate: 140.0 MB/s
Took 0.669 seconds to write to a 100 MB, file rate: 156.9 MB/s
Took 0.569 seconds to read to a 100 MB file, rate: 184.6 MB/s
Took 1.585 seconds to write to a 250 MB, file rate: 165.5 MB/s
Took 1.274 seconds to read to a 250 MB file, rate: 206.0 MB/s
Took 2.513 seconds to write to a 500 MB, file rate: 208.8 MB/s
Took 2.332 seconds to read to a 500 MB file, rate: 225.1 MB/s
Took 5.094 seconds to write to a 1000 MB, file rate: 206.0 MB/s
Took 5.041 seconds to read to a 1000 MB file, rate: 208.2 MB/s
Took 11.509 seconds to write to a 2001 MB, file rate: 182.4 MB/s
Took 9.681 seconds to read to a 2001 MB file, rate: 216.8 MB/s

Windowsマシンで、メモリを多く搭載している場合。

Took 0.376 seconds to write to a 50 MB, file rate: 139.7 MB/s
Took 0.401 seconds to read to a 50 MB file, rate: 131.1 MB/s
Took 0.517 seconds to write to a 100 MB, file rate: 203.1 MB/s
Took 0.520 seconds to read to a 100 MB file, rate: 201.9 MB/s
Took 1.344 seconds to write to a 250 MB, file rate: 195.4 MB/s
Took 1.387 seconds to read to a 250 MB file, rate: 189.4 MB/s
Took 2.368 seconds to write to a 500 MB, file rate: 221.8 MB/s
Took 2.454 seconds to read to a 500 MB file, rate: 214.1 MB/s
Took 4.985 seconds to write to a 1001 MB, file rate: 210.7 MB/s
Took 5.132 seconds to read to a 1001 MB file, rate: 204.7 MB/s
Took 10.276 seconds to write to a 2003 MB, file rate: 204.5 MB/s
Took 9.964 seconds to read to a 2003 MB file, rate: 210.9 MB/s
解説 (6)

まず試してみたいのは、BufferedReaderとBufferedWriterのバッファサイズを大きくすることです。デフォルトのバッファサイズは文書化されていませんが、少なくともOracle VMでは8192文字で、これはあまりパフォーマンス上の利点をもたらさないでしょう。

もし、ファイルのコピーを取るだけなら(そして、データへの実際のアクセスは必要ない)、私なら、リーダー/ライター方式をやめて、バッファとしてバイト配列を使ったInputStreamとOutputStreamで直接作業します。

FileInputStream fis = new FileInputStream("d:/test.txt");
FileOutputStream fos = new FileOutputStream("d:/test2.txt");
byte[] b = new byte[bufferSize];
int r;
while ((r=fis.read(b))>=0) {
    fos.write(b, 0, r);         
}
fis.close();
fos.close();

または実際にNIOを使用します。

FileChannel in = new RandomAccessFile("d:/test.txt", "r").getChannel();
FileChannel out = new RandomAccessFile("d:/test2.txt", "rw").getChannel();
out.transferFrom(in, 0, Long.MAX_VALUE);
in.close();
out.close();

しかし、異なるコピー方式をベンチマークした場合、実装の違いよりもベンチマークの実行ごとの差(時間)の方がはるかに大きくなりました。ここでは、I/Oキャッシュ(OSレベルとハードディスクキャッシュの両方)が大きな役割を果たし、何が速いかを言うのは非常に難しい。私のハードウェアでは、BufferedReaderとBufferedWriterを使用して1GBのテキストファイルを1行ずつコピーすると、ある実行では5秒未満、別の実行では30秒以上かかっています。

解説 (4)

私は、java.nioパッケージのクラスを見ることをお勧めします。 ソケットの場合、ノンブロッキングIOの方が速いかもしれません。

http://docs.oracle.com/javase/6/docs/api/java/nio/package-summary.html

この記事には、それが真実であるとするベンチマークがあります'。

http://vanillajava.blogspot.com/2010/07/java-nio-is-faster-than-java-io-for.html

解説 (3)