For a server side app I’ll have to create lots of PNG.
I’ve written a little benchmark that simulates some aspects of what it’ll have to do.
The benchmark creates a single BufferedImage (currently TYPE_INT_ARGB) clears it each frame and writes some text (slightly different for each frame) into it and encodes it as a PNG into a ByteArrayOutputStream. (I’ve verified that writing to files takes only slightly longer, which means that the JIT don’t try to fool me).
I’ve ported the Benchmark to .NET 2.0, Java 1.5. and Java 1.6B Java’s ImageIO PNG encoder is very slow and I’ve already found PngEncoder from Objectplanet (commercial, but 100$ would fit into the budget) which is faster.
For 100 Images in 300 x 300 with some ĺines of text I got the following results (in milliseconds):
.NET 2.0 Beta
Rendering 100 frames: 296,875
PNG encoding 100 frames: 578,125
JDK 1.5, ImageIO
Rendering 100 frames: 230
PNG encoding 100 frames: 14111
JDK 1.5, JAI 1.1.3-alpha
Rendering 100 frames: 230
PNG encoding 100 frames: 20211
JDK 1.5 -server, PngEncoder
Rendering 100 frames: 223
PNG encoding 100 frames: 2043
JDK 1.6 -server B39, PngEncoder
Rendering 100 frames: 246
PNG encoding 100 frames: 1568
(Conslusion: .NET 2.0’s PNG encoder is much faster than the Java alternatives )
So here come my questions:
- Are there any tricks to make ImageIO’s PNGEncoder faster? My code for saving looks like that:
public PNGSaver()
{
Iterator<ImageWriter> it=ImageIO.getImageWritersByFormatName(format);
if (!it.hasNext())
{
throw new IllegalStateException("No PNG ImageWriter found");
}
writer=it.next();
}
public void saveImage(OutputStream os,BufferedImage img) throws IOException
{
image = new IIOImage(img, null, null);
writer.setOutput(new MemoryCacheImageOutputStream(os));
writer.write(null,image,iwp);
}
-
Can yourecommend other fast Java PNG encoders?
-
Do you have any other performance tricks for fast server side image generators?
Thanks,
Stefan