49

ディスクへのバイトの読み取り、処理、および書き込みを担当するモジュールがあります。バイトは UDP 経由で受信され、個々のデータグラムが組み立てられた後、処理されてディスクに書き込まれる最終的なバイト配列は、通常 200 バイトから 500,000 バイトの間になります。場合によっては、アセンブリ後に 500,000 バイトを超えるバイト配列が存在することがありますが、これらは比較的まれです。

私は現在、FileOutputStreamwrite(byte\[\])メソッドを使用しています。また、パラメーターとしてバッファー サイズを受け入れるコンストラクターを使用するなど、 をFileOutputStreamaにラップすることも試しています。BufferedOutputStream

を使用するBufferedOutputStreamとパフォーマンスがわずかに向上する傾向にあるようですが、さまざまなバッファー サイズを試し始めたばかりです。使用するサンプル データのセットは限られています (アプリケーションを介してパイプできるサンプル実行からの 2 つのデータ セット)。書き込み中のデータについて知っている情報を考慮して、ディスク書き込みを減らし、ディスク書き込みのパフォーマンスを最大化するための最適なバッファー サイズを計算するために適用できる一般的な経験則はありますか?

4

2 に答える 2

35

BufferedOutputStream は、書き込みがバッファー サイズ (8 KB など) より小さい場合に役立ちます。大規模な書き込みの場合、それは役に立ちませんし、さらに悪化させることもありません。すべての書き込みがバッファ サイズよりも大きい場合、またはすべての書き込み後に常に flush() を実行する場合、バッファは使用しません。ただし、書き込みの大部分がバッファ サイズよりも小さく、毎回 flush() を使用しない場合は、使用する価値があります。

バッファー サイズを 32 KB 以上に増やすと、わずかに改善されるか、悪化することがあります。YMMV


BufferedOutputStream.write のコードが役立つ場合があります。

/**
 * Writes <code>len</code> bytes from the specified byte array
 * starting at offset <code>off</code> to this buffered output stream.
 *
 * <p> Ordinarily this method stores bytes from the given array into this
 * stream's buffer, flushing the buffer to the underlying output stream as
 * needed.  If the requested length is at least as large as this stream's
 * buffer, however, then this method will flush the buffer and write the
 * bytes directly to the underlying output stream.  Thus redundant
 * <code>BufferedOutputStream</code>s will not copy data unnecessarily.
 *
 * @param      b     the data.
 * @param      off   the start offset in the data.
 * @param      len   the number of bytes to write.
 * @exception  IOException  if an I/O error occurs.
 */
public synchronized void write(byte b[], int off, int len) throws IOException {
    if (len >= buf.length) {
        /* If the request length exceeds the size of the output buffer,
           flush the output buffer and then write the data directly.
           In this way buffered streams will cascade harmlessly. */
        flushBuffer();
        out.write(b, off, len);
        return;
    }
    if (len > buf.length - count) {
        flushBuffer();
    }
    System.arraycopy(b, off, buf, count, len);
    count += len;
}
于 2012-01-03T13:30:11.473 に答える