[英]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.writeObject
比ObjectOutputStream.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.