1

I am using im4java in my project for various image processing techniques such as cropping, resizing, filling and rotating. Before this I was using the java.lang.Runtime.exec to run commands in the command prompt. Now when I bench marked the two methods, they almost gave the same result! I don't need the parallel processing feature of the im4java, because the output of one process is the input of the other, hence sequential. In my case, it provides just one advantage: ease of use. And that's all.

Do you think I am missing something or lacking somewhere in my code? Any suggestions would be of great help. Thanks in advance!

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
gursahib.singh.sahni
  • 1,559
  • 3
  • 26
  • 51
  • If you just did `long start = System.currentTimeMillis(); process(); System.out.println("Took " + System.currentTimeMillis()-start);`, then that's not how you benchmark on the JVM (not even close). – Marko Topolnik Aug 09 '13 at 07:42
  • Sir what's the correct way to do it? – gursahib.singh.sahni Aug 09 '13 at 07:44
  • Well, the "pro" way is to use a bencmarking framework such as Google Caliper or Oracle jmh. A poor man's alternative would be to repeat your processing in an endless loop, displaying for each iteration the time it took. And use `System.nanoTime`. Then you'll see to what number these times converge. The biggest thing is waiting for all your code to be JIT-compiled. – Marko Topolnik Aug 09 '13 at 07:46
  • I think im4java is using `Runtime.exec` internally to delegate image processing commands to ImageMagick. Because of this, it's only natural that the benchmark showing similar performance. – Harald K Aug 09 '13 at 07:52
  • By the way sir, you have any idea regarding the performance of im4java ? you think it will be the same as the `exec` command ? – gursahib.singh.sahni Aug 09 '13 at 07:53
  • @haraldK - alright sir – gursahib.singh.sahni Aug 09 '13 at 07:53
  • [link]http://stackoverflow.com/questions/3943431/execute-an-external-command-in-java. Says uses ProcessBuilder internally -_- – gursahib.singh.sahni Aug 09 '13 at 07:58
  • @anonymous Ok, I stand corrected. But `Runtime.exec` also uses `ProcessBuilder` under the hood, so I still don't think there's significant performance differences (though I haven't measured). In any case it's a wrapper for the command line interface. – Harald K Aug 09 '13 at 10:46

1 Answers1

2

Yes, you are right that using im4java along only gives you the same performance as you did with Runtime.exec. Or maybe slower because it has the command building and translation layer.

But if your applicaiton is to convert a lot of images in one run (or large scale concurrent processing), then please let me shamelessly introduce the gm4java, which can help to improve the performance.

gm4java makes use of recently developed batch/interactive mode of GraphicsMagick to aovid starting a new GraphicsMagick process again and again for every command. It can be used as supplement to im4java or just execute the command you built for your Runtime.exec.