Slowdown when calling a plugin from within a loop

Posted by Eric Olson-3 on
URL: http://imagej.273.s1.nabble.com/Slowdown-when-calling-a-plugin-from-within-a-loop-tp3696849.html

I've been using ImageJ for a few years now, but just wrote my first
plugin. I just came across a strange issue that I don't really
understand and was hoping someone might have some insight. I guess this
is more of a Java question than an ImageJ question.

My plugin just converts a batch of files from one format to another and
is based around the DM3_Reader plugin
<http://rsb.info.nih.gov/ij/plugins/DM3_Reader.html> by Gregory
Jefferis.  I found that my plugin got progressively slower and slower
when it was looping over a directory of images. The first image took
about 1-2 seconds to convert, by the time it got to the seventh image,
it was taking 30-40 seconds.

Here is a simplification of what I was originally trying to do:

public class myConverter1{
   // DM3_Reader extends ImagePlus
   private DM3_Reader myReader = new DM3_Reader();

   public void run(String args) {
      for(int i=0; i<filename.length; i++){
         myReader.run (filename(i));

         fileSaver = new FileSaver(myReader);
         fileSaver.saveAsTiff(outputFile);
      }
   }
}


This change fixes the problem, every image takes only 1-2 seconds to
convert, but I don't really understand why:

public class myConverter2{
   public void run(String args) {
      for(int i=0; i<filename.length; i++){
         // DM3_Reader extends ImagePlus
         DM3_Reader myReader = new DM3_Reader();
         myReader (filename)

         fileSaver = new FileSaver(myReader);
         fileSaver.saveAsTiff(outputFile);
      }
   }
}

The only significant change is that myReader now goes out of scope at
the end of the for loop. I don't seeI don't see why this would have such
a large effect on the processing time. Am I just being daft or can
someone share some insight?

--
Eric Olson
Postdoctoral Research Associate, University of Illinois at Urbana-Champaign
[hidden email], 217-244-2117 (voice), 217-244-2278 (fax)