2014-12-06 103 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