2014-12-06 100 views
0

我最近開發了我自己的文件解析類,名爲BufferedParseStream,並用它來解碼PNG圖像。我一直在比較它的性能與開源項目PNGJ,並且已經看到,對於較小的圖像尺寸,PNGJ可以比我自己的實現快兩倍。我認爲這與使用BufferedInputStream時的實施開銷相關,因爲PNGJ會自行推出equivalent有沒有高性能文件解析的設計模式?

是否有任何現有的指導高性能文件解析的設計模式,如int,float等基元?

public class BufferedParseStream extends BufferedInputStream { 

private final ByteBuffer mByteBuffer; 

public BufferedParseStream(final InputStream pInputStream, final int pBufferSize) { 
    super(pInputStream, pBufferSize); 
    /* Initialize the ByteBuffer. */ 
    this.mByteBuffer = DataUtils.delegateNative(new byte[8]); 
} 

private final void buffer(final int pNumBytes) throws IOException { 
    /* Read the bytes into the ByteStorage. */ 
    this.read(this.getByteBuffer().array(), 0, pNumBytes); 
    /* Reset the ByteBuffer Location. */ 
    this.getByteBuffer().position(0); 
} 

public final char parseChar() throws IOException { 
    /* Read a single byte. */ 
    this.buffer(DataUtils.BYTES_PER_CHAR); 
    /* Return the corresponding character. */ 
    return this.getByteBuffer().getChar(); 
} 

public final int parseInt() throws IOException { 
    /* Read four bytes. */ 
    this.buffer(DataUtils.BYTES_PER_INT); 
    /* Return the corresponding integer. */ 
    return this.getByteBuffer().getInt(); 
} 

public final long parseLong() throws IOException { 
    /* Read eight bytes. */ 
    this.buffer(DataUtils.BYTES_PER_LONG); 
    /* Return the corresponding long. */ 
    return this.getByteBuffer().getLong(); 
} 

public final void setParseOrder(final ByteOrder pByteOrder) { 
    this.getByteBuffer().order(pByteOrder); 
} 

private final ByteBuffer getByteBuffer() { 
    return this.mByteBuffer; 
} 

}

+0

我不認爲我們可以在這種情況下談論設計模式,它更傾向於算法優化,這是大多數時間算法特定的。嘗試確定哪部分代碼需要花費太多時間並修復它 – Dici 2014-12-06 16:57:10

+0

我明白,抱歉讓術語混淆不清。你看到有關'BufferedParseStream'的特別漏洞嗎? – 2014-12-06 16:58:22

+0

不是特別的,但我不太瞭解這個類和方法。這是你寫的代碼的唯一部分嗎?沒有解碼PNG圖像的類嗎?如果你寫了它,這是最有可能效率不高的部分 – Dici 2014-12-06 17:08:55

回答

1

的Java NIO應比使用輸入流快,你提出你的類似乎有些奇怪,我(可能只是我雖然:)),因爲它對字節緩衝區的頂部一個額外層我認爲這不是必需的。

您應該直接使用字節緩衝區,它有一個getInt,getFloat方法,您可以將其直接送入所需的變量。

我認爲,儘管您的性能問題可能在PNG解碼器代碼中,正如其他人已經提到的那樣。你應該發佈這個進一步的分析

+0

我沒有意識到我正在增加額外的複雜層!我會重構我的工作以消除冗餘組件。 就一般的PNG解碼器而言,我提出了這個問題,因爲其他流式傳輸方法我採用了,外部代碼大致相同。這是因爲解析像素時可以使用的算法選擇有限,並將這些算法流式傳輸到緩衝區。謝謝你的建議。 (也歡迎SO!) – 2014-12-07 18:23:46