From: Thomas J. <ntm...@gm...> - 2005-07-23 01:51:55
|
On 7/22/05, Toby Donaldson <tj...@sf...> wrote: > Can anyone help me to figure out why the code below is so slow? It > converts and image to grayscale pixel by pixel, but when I've run it > (on two different computers --- Windows and Mac OS X), it takes 20 or > more seconds to process a medium sized image. I would have expected > it take less than a second or so. I've run the given code snippet on my Linux box (Sun Java 1.5.0) and a G4 laptop (Sun Java 1.4.2), testing against csimage/pictures/sumo.jpg and have been unable to reproduce this behaviour. Just to double-check, this is all supposed to be done with the classes belonging to the stock JVM right? The necessary imports in question being java.awt.image.BufferedImage, java.io.File, and javax.imageio.ImageIO? It might be prudent to give it a run with the profiler ( java -Xprof className ) in the hopes that some light will be shed on exactly where performance is suffering. Additonally, have you tried running this test standalone? It may be possible that the JVM is trying to garbage collect while test4 is running. > The bit-twiddling code should be quite efficient, since such > operations are (hopefully) converted to single assembly-language > instructions. The resulting operations can be viewed with a Java Compiler such as Jad (The fast JAva Decompiler - http://www.kpdus.com/jad.html ). By the looks of it, the code is as fast as it's going to get: int l2 =3D i2 << 8; // 80 154:iload 10 // 81 156:bipush 8 // 82 158:ishl =20 // 83 159:istore 13 > I've been using JDK 5.0 (aka 1.5), so maybe that's causing the problem? It might be worth verifying that the classpath is set correctly, and that the system hasn't somehow reverted back to an older or different version of Java without your intervention. Bear in mind that it's also possible to have the 1.3 JDK doing the compiling and the 1.5 JRE actually executing the program. Are the following conversions really necessary? Would "(r + g + b)/3;" suffice, as the extra precision is just being lost anyway? > int lum =3D (int) ((r + g + b) / 3.0); Hope this helps. |