简体   繁体   English

为什么在写字节数组时ObjectOutputStream.writeObject比写字节快?

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

I did a small benchmark test and found that ObjectOutputStream.writeObject is faster than ObjectOutputStream.write(byte[] bytes) but I can't seem to find a possible explanation as under the hood, writeObject will call ObjectOutputStream.write(byte[] bytes) indirectly 我做了一个小型基准测试,发现ObjectOutputStream.writeObjectObjectOutputStream.write(byte[] bytes)快,但是我似乎找不到可能的解释,因为在幕后, writeObject将调用ObjectOutputStream.write(byte[] bytes)间接

Test code 测试码

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");
    }
}

Output 输出量

write byte[] took: 15445 micros 写入byte []需要:15445微米

writeObject took: 3111 micros writeObject需要:3111微米

ObjectOutputStream.writeObject() conserves written objects. ObjectOutputStream.writeObject()保存写入的对象。 If you've already written that object, it only writes a handle to the same object, not the entire object. 如果您已经编写了该对象,则它只会将一个句柄写入同一对象,而不是整个对象。

I wrote a slight modification of your code: 我对您的代码做了一些修改:

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;
  }
}

Now the result is 现在的结果是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试在类之间写入 object 时 ObjectOutputStream.writeObject() 冻结 - ObjectOutputStream.writeObject() freezing when trying to write object between classes 为什么ObjectOutputStream.writeObject不采用Serializable? - Why does ObjectOutputStream.writeObject not take a Serializable? NockPointerException当Mockito嘲笑ObjectOutputStream.writeObject吗? - NullPointerException when Mockito mocking ObjectOutputStream.writeObject? 编写可序列化的对象时,ObjectOutputStream.writeObject()(在socket.getOutputStream()顶部)抛出断线(IO异常) - ObjectOutputStream.writeObject() (on top of socket.getOutputStream()) throws broken pipe (IO Exception) when writing a serializable object 如何模拟objectOutputStream.writeObject()? - How to mock objectOutputStream.writeObject()? ObjectOutputStream.writeObject 对文件线程安全吗? - Is ObjectOutputStream.writeObject to a file thread safe? 使用ObjectOutputStream.writeObject()无法进行数据传输 - data transfer not working using ObjectOutputStream.writeObject() 我应该同步 ObjectOutputStream.writeObject(Object) 吗? - Should I synchronize ObjectOutputStream.writeObject(Object)? Java,ObjectOutputStream.writeObject将随机符号打印到文件 - Java, ObjectOutputStream.writeObject prints random symbols to file 为什么一次读取和读取字节数组比读取一个字节快? - Why reading and array of bytes at a time is faster than reading one byte?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM