// This macro tests the speed of the ROI Manager
// running in batch mode and not in batch mode.

  // Make sure the ROI Manager is closed if the macro, like
  // this one, runs entirely in batch mode. ImageJ will use
  // an invisible ROI Manager that will run faster and more
  // reliably. Replace this code with roiManager("reset") in
  // macros that do not run entirely in batch mode.
  if (isOpen("ROI Manager")) {
     selectWindow("ROI Manager");
     run("Close");
  }

  t1 = test(true);
  t2 = test(false);
  showMessage("Speed Test", "This macro runs "+round(t2/t1)+" times faster in batch mode");
  exit;
  
  function test(batchMode) {
     width = 1000;
     height = 1000;
     n = 250;
     areas = newArray(n);
     means = newArray(n);
     setBatchMode(batchMode);
     roiManager("reset");
     newImage("Untitled", "8-bit ramp", width, height, 1);
     start = getTime;
     for (i=0; i<n; i++) {
         w = random()*width/10+5;
         h = random()*width/10+5;
         x = random()*width-w/10;
         y = random()*height-h/10;
         makeOval(x, y, w, h);
         getStatistics(areas[i], means[i]);
         roiManager("add");
      }
      for (i=0; i<n; i++) {
         roiManager('select', i);
         getStatistics(area, mean);
         if (area!=areas[i] || mean!=means[i])
            print("ERROR:", i, area, areas[i], mean, means[i]);
         run("Add Selection...", "stroke=red");
      }
     if (batchMode) close;
     time = getTime-start;
     return time;
  }