我最近开发了我自己的文件解析类,名为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;
}
}
我不认为我们可以在这种情况下谈论设计模式,它更倾向于算法优化,这是大多数时间算法特定的。尝试确定哪部分代码需要花费太多时间并修复它 – Dici 2014-12-06 16:57:10
我明白,抱歉让术语混淆不清。你看到有关'BufferedParseStream'的特别漏洞吗? – 2014-12-06 16:58:22
不是特别的,但我不太了解这个类和方法。这是你写的代码的唯一部分吗?没有解码PNG图像的类吗?如果你写了它,这是最有可能效率不高的部分 – Dici 2014-12-06 17:08:55