2015-09-04 84 views
0

我做了一個小的基準測試,發現ObjectOutputStream.writeObject快於ObjectOutputStream.write(byte[] bytes),但我似乎無法找到一個可能的解釋是引擎蓋下,writeObject將調用ObjectOutputStream.write(byte[] bytes)間接爲什麼ObjectOutputStream.writeObject在寫入字節數組時比寫入字節更快?

測試代碼

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

輸出

寫字節[]了:15445萬分之一

的writeObject了:3111萬分之一

回答

5

ObjectOutputStream.writeObject()節省寫對象。如果你已經寫過那個對象,它只會寫一個句柄給同一個對象,而不是整個對象。

+0

事實上,你是正確的。我將代碼替換爲每次都有一個新數組,寫入字節數組現在比writeObject快。我完全忘記了writeObject所做的緩存 –

1

我寫你的代碼稍加修改:

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 
+0

謝謝你的回答。的確,我完全忘記了writeObject中的緩存機制 –

相關問題