簡體   English   中英

在Java中將整數數組寫入文件的最快方法?

[英]Fastest way to write an array of integers to a file in Java?

正如標題所說,我正在尋找將整數數組寫入文件的最快方法。 陣列的大小會有所不同,並且實際上可以包含2500到25 000 000個整數。

這是我目前使用的代碼:

DataOutputStream writer = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(filename)));

for (int d : data)
  writer.writeInt(d);

鑒於DataOutputStream有一個寫字節數組的方法,我嘗試將int數組轉換為字節數組,如下所示:

private static byte[] integersToBytes(int[] values) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    for (int i = 0; i < values.length; ++i) {
        dos.writeInt(values[i]);
    }

    return baos.toByteArray();
}

和這樣:

private static byte[] integersToBytes2(int[] src) {
    int srcLength = src.length;
    byte[] dst = new byte[srcLength << 2];

    for (int i = 0; i < srcLength; i++) {
        int x = src[i];
        int j = i << 2;
        dst[j++] = (byte) ((x >>> 0) & 0xff);
        dst[j++] = (byte) ((x >>> 8) & 0xff);
        dst[j++] = (byte) ((x >>> 16) & 0xff);
        dst[j++] = (byte) ((x >>> 24) & 0xff);
    }
    return dst;
}

兩者似乎都會提高速度,約為5%。 我沒有嚴格測試它們來證實這一點。

是否有任何技術可以加速此文件寫入操作,或者有關Java IO寫入性能的最佳實踐的相關指南?

我看了三個選項:

  1. 使用DataOutputStream ;
  2. 使用ObjectOutputStream (對於Serializable對象, int[]是);
  3. 使用FileChannel

結果是

DataOutputStream wrote 1,000,000 ints in 3,159.716 ms
ObjectOutputStream wrote 1,000,000 ints in 295.602 ms
FileChannel wrote 1,000,000 ints in 110.094 ms

所以NIO版本是最快的。 它還具有允許編輯的優點,這意味着您可以輕松地更改一個int,而ObjectOutputStream需要讀取整個數組,修改它並將其寫入文件。

代碼如下:

private static final int NUM_INTS = 1000000;

interface IntWriter {
  void write(int[] ints);
}

public static void main(String[] args) {
  int[] ints = new int[NUM_INTS];
  Random r = new Random();
  for (int i=0; i<NUM_INTS; i++) {
    ints[i] = r.nextInt();
  }
  time("DataOutputStream", new IntWriter() {
    public void write(int[] ints) {
      storeDO(ints);
    }
  }, ints);
  time("ObjectOutputStream", new IntWriter() {
    public void write(int[] ints) {
      storeOO(ints);
    }
  }, ints);
  time("FileChannel", new IntWriter() {
    public void write(int[] ints) {
      storeFC(ints);
    }
  }, ints);
}

private static void time(String name, IntWriter writer, int[] ints) {
  long start = System.nanoTime();
  writer.write(ints);
  long end = System.nanoTime();
  double ms = (end - start) / 1000000d;
  System.out.printf("%s wrote %,d ints in %,.3f ms%n", name, ints.length, ms);
}

private static void storeOO(int[] ints) {
  ObjectOutputStream out = null;
  try {
    out = new ObjectOutputStream(new FileOutputStream("object.out"));
    out.writeObject(ints);
  } catch (IOException e) {
    throw new RuntimeException(e);
  } finally {
    safeClose(out);
  }
}

private static void storeDO(int[] ints) {
  DataOutputStream out = null;
  try {
    out = new DataOutputStream(new FileOutputStream("data.out"));
    for (int anInt : ints) {
      out.write(anInt);
    }
  } catch (IOException e) {
    throw new RuntimeException(e);
  } finally {
    safeClose(out);
  }
}

private static void storeFC(int[] ints) {
  FileOutputStream out = null;
  try {
    out = new FileOutputStream("fc.out");
    FileChannel file = out.getChannel();
    ByteBuffer buf = file.map(FileChannel.MapMode.READ_WRITE, 0, 4 * ints.length);
    for (int i : ints) {
      buf.putInt(i);
    }
    file.close();
  } catch (IOException e) {
    throw new RuntimeException(e);
  } finally {
    safeClose(out);
  }
}

private static void safeClose(OutputStream out) {
  try {
    if (out != null) {
      out.close();
    }
  } catch (IOException e) {
    // do nothing
  }
}

我會使用nio包和ByteBuffer FileChannel 這種方法似乎(在我的計算機上)提高了2到4倍的寫入性能

程序輸出:

normal time: 2555
faster time: 765

這是該計划:

public class Test {

    public static void main(String[] args) throws IOException {

        // create a test buffer
        ByteBuffer buffer = createBuffer();

        long start = System.currentTimeMillis();
        {
            // do the first test (the normal way of writing files)
            normalToFile(new File("first"), buffer.asIntBuffer());
        }
        long middle = System.currentTimeMillis(); 
        {
            // use the faster nio stuff
            fasterToFile(new File("second"), buffer);
        }
        long done = System.currentTimeMillis();

        // print the result
        System.out.println("normal time: " + (middle - start));
        System.out.println("faster time: " + (done - middle));
    }

    private static void fasterToFile(File file, ByteBuffer buffer) 
    throws IOException {

        FileChannel fc = null;

        try {

            fc = new FileOutputStream(file).getChannel();
            fc.write(buffer);

        } finally {

            if (fc != null)
                fc.close();

            buffer.rewind();
        }
    }

    private static void normalToFile(File file, IntBuffer buffer) 
    throws IOException {

        DataOutputStream writer = null;

        try {
            writer = 
                new DataOutputStream(new BufferedOutputStream(
                        new FileOutputStream(file)));

            while (buffer.hasRemaining())
                writer.writeInt(buffer.get());

        } finally {
            if (writer != null)
                writer.close();

            buffer.rewind();
        }
    }

    private static ByteBuffer createBuffer() {
        ByteBuffer buffer = ByteBuffer.allocate(4 * 25000000);
        Random r = new Random(1);

        while (buffer.hasRemaining()) 
            buffer.putInt(r.nextInt());

        buffer.rewind();

        return buffer;
    }
}

我認為你應該考慮使用文件通道(java.nio庫)而不是普通流(java.io)。 一個很好的起點是這個有趣的討論: Java NIO FileChannel與FileOutputstream的性能/實用性

以及下面的相關評論。

干杯!

編寫int []的主要改進是:

  • 增加緩沖區大小。 大小適合大多數流,但使用更大的緩沖區可以更快地訪問文件。 這可以產生10-20%的改善。

  • 使用NIO和直接緩沖區。 這允許您編寫32位值而無需轉換為字節。 這可能會帶來5%的改善。

順便說一句:你應該能夠每秒寫入至少1000萬個int值。 使用磁盤緩存,您可以將其增加到每秒2億。

基准應該每隔一段時間重復一次,不是嗎? :)修復了一些錯誤並添加了我自己的寫入變體后,這是我在運行Windows 10的ASUS ZenBook UX305上運行基准測試時獲得的結果(以秒為單位的時間):

Running tests... 0 1 2
Buffered DataOutputStream           8,14      8,46      8,30
FileChannel alt2                    1,55      1,18      1,12
ObjectOutputStream                  9,60     10,41     11,68
FileChannel                         1,49      1,20      1,21
FileChannel alt                     5,49      4,58      4,66

以下是在同一台計算機上運行的結果,但是使用Arch Linux並且切換了寫入方法的順序:

Running tests... 0 1 2
Buffered DataOutputStream          31,16      6,29      7,26
FileChannel                         1,07      0,83      0,82
FileChannel alt2                    1,25      1,71      1,42
ObjectOutputStream                  3,47      5,39      4,40
FileChannel alt                     2,70      3,27      3,46

每個測試都寫了一個800mb的文件。 無緩沖的DataOutputStream占用了很長時間,因此我將其排除在基准測試之外。

如圖所示,使用文件通道寫入仍然勝過所有其他方法的廢話,但是字節緩沖區是否是內存映射是很重要的。 沒有內存映射文件通道寫入需要3-5秒:

var bb = ByteBuffer.allocate(4 * ints.length);
for (int i : ints)
    bb.putInt(i);
bb.flip();
try (var fc = new FileOutputStream("fcalt.out").getChannel()) {
    fc.write(bb);
}

使用內存映射,時間減少到0.8到1.5秒之間:

try (var fc = new RandomAccessFile("fcalt2.out", "rw").getChannel()) {
    var bb = fc.map(READ_WRITE, 0, 4 * ints.length);
    bb.asIntBuffer().put(ints);
}

但請注意,結果依賴於順序。 特別是在Linux上。 看起來內存映射方法不會完全寫入數據,而是將作業請求卸載到OS並在完成之前返回。 這種行為是否可取取決於具體情況。

內存映射也可能導致OutOfMemory問題,因此它並不總是正確的工具。 使用java.nio.MappedByteBuffer時防止OutOfMemory

這是我的基准代碼版本: https//gist.github.com/bjourne/53b7eabc6edea27ffb042e7816b7830b

數組是Serializable - 你不能只使用writer.writeObject(data); writeInt單個writeInt調用更快。

如果您對輸出數據格式有其他要求而不是檢索到int[] ,那么這是一個不同的問題。

暫無
暫無

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

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