簡體   English   中英

為什么在寫字節數組時ObjectOutputStream.writeObject比寫字節快?

[英]Why is ObjectOutputStream.writeObject faster than write bytes when writing a byte array?

我做了一個小型基准測試,發現ObjectOutputStream.writeObjectObjectOutputStream.write(byte[] bytes)快,但是我似乎找不到可能的解釋,因為在幕后, writeObject將調用ObjectOutputStream.write(byte[] bytes)間接

測試碼

public static void main(String[] args) throws Exception {
    byte[] bytes = new byte[10000];
    for (int i = 0; i < 10000; ++i) {
        bytes[i] = (byte) (i % 256);
    }

    ByteArrayOutputStream out2 = new ByteArrayOutputStream();
    try(ObjectOutputStream ostream2 = new ObjectOutputStream(out2)) {

        for (int i = 0; i < 10000; ++i) {
            ostream2.writeInt(bytes.length);
            ostream2.write(bytes, 0, bytes.length);
        }

        out2.reset();

        long start = System.nanoTime();
        for (int i = 0; i < 10000; ++i) {
            ostream2.writeInt(bytes.length);
            ostream2.write(bytes, 0, bytes.length);
        }
        long end = System.nanoTime();

        System.out.println("write byte[] took: " + ((end - start) / 1000) + " micros");
    }

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try(ObjectOutputStream ostream = new ObjectOutputStream(out)) {
        for (int i = 0; i < 10000; ++i) {
            ostream.writeObject(bytes);
        }

        out.reset();

        long start = System.nanoTime();
        for (int i = 0; i < 10000; ++i) {
            ostream.writeObject(bytes);
        }
        long end = System.nanoTime();

        System.out.println("writeObject took: " + ((end - start) / 1000) + " micros");
    }
}

輸出量

寫入byte []需要:15445微米

writeObject需要:3111微米

ObjectOutputStream.writeObject()保存寫入的對象。 如果您已經編寫了該對象,則它只會將一個句柄寫入同一對象,而不是整個對象。

我對您的代碼做了一些修改:

public class Test {

  public static final int REPS = 10000;

  public static void main(String argv[]) throws IOException {
    ByteArrayOutputStream out2 = new ByteArrayOutputStream();
    try (ObjectOutputStream ostream2 = new ObjectOutputStream(out2)) {
      writeBytes(ostream2);
      out2.reset();
      long start = System.nanoTime();
      writeBytes(ostream2);
      long end = System.nanoTime();
      System.out.println("write byte[] took: " + ((end - start) / 1000) + " micros");
    }

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try (ObjectOutputStream ostream = new ObjectOutputStream(out)) {
      writeObject(ostream);
      out.reset();
      long start = System.nanoTime();
      writeObject(ostream);
      long end = System.nanoTime();
      System.out.println("writeObject took: " + ((end - start) / 1000) + " micros");
    }
  }

  private static void writeObject(ObjectOutputStream ostream) throws IOException {
    for (int i = 0; i < REPS; ++i) {
      final byte[] bytes = bytes();
      ostream.writeObject(bytes);
    }
  }

  private static void writeBytes(ObjectOutputStream ostream2) throws IOException {
    for (int i = 0; i < REPS; ++i) {
      final byte[] bytes = bytes();
      ostream2.writeInt(bytes.length);
      ostream2.write(bytes, 0, bytes.length);
    }
  }

  static byte[] bytes() {
    byte[] bytes = new byte[REPS];
    for (int i = 0; i < REPS; ++i) {
      bytes[i] = (byte) i;
    }
    return bytes;
  }
}

現在的結果是

write byte[] took: 51697 micros
writeObject took: 57203 micros

暫無
暫無

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

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