"Waited 5000ms for" issue

Hi everyone,

I have encountered a problem with four nested for loops, where for each cell (X, Y), I look into all other cells to add up the distances to certain type of cells. The result is something like metaball.
The entire nest of for loops takes more than 5000ms to complete calculations and the program fails to run.

      for (int i = 0; i<cols; i++) { 
        for (int j = 0; j<rows; j++) {
          sum = 0;
          if (cell[i][j].analisisTipo == 2) { // Each cell has a type (analisisTipo), type 2 is to those we want to measure distances, they are scattered around the canvas
            for (int n = 0; n<cols; n++) {
              for (int m = 0; m<rows; m++) {
                if (cell[i][j] != cell[n][m]) {    
                  float d = dist(cell[i][j].coord.x, cell[i][j].coord.y, cell[n][m].coord.x, cell[n][m].coord.y);
                }
              }
            }
          }
          sum += 1 / d;
          cell[i][j].analisisLuz = sum;
        }
      }

I undersand that my problem might be related to code optimization. One way would be to split the parent for loop into N number of for loops, maybe while loops would work as well. In this case it is solved when cell size is increased with a lower resolution so the number of iterations decreases (so it is not a problem about infinite loops).
Let’s suppose that in the middle of the nested for loops we have a rather complicated function that takes a little bit more of time than dist() to return a value, as I am not interested in performance, only in static images; I would like to force the program to take its time to complete calculations whether or not the code is optimized enough.

Sorry for not posting the whole code, but in some computers it works even with high resolution. But what if I had 7 nested for loops and wanted it to take 1 minute to calculate?

Thanks in advance, Roger.

with what kind of mega arrays you work?
as from the code can not see what cols ? rows ? you talk about.

but as you see 5s in your draw loop? no use to measure FPS,

try to build in a millis timer
and check how long for the 4 nested loops
( with disabled calculation … )
and how long with calculation.


but looking at the code i begin to doubt the story,

coord_.y

should give a error, or you have that variable in the cell array class?


the

float d  

inside the for loop


and the logic, is that correct?
change d if analisisTipo == 2
but calc sum = f(d) anyhow???

Yes, thank you for your response. I have to say that code is not exactly what runs in my sketch, this is an except only for context, since the actual problem is not a coding problem but an “enviromental” one. Because the coding enviroment doesn’t allow me to spend more than 5000ms in a for loop.
I can figure out ways of solving this particular case, as said in the topic, but with more complicated functions the problem would appear again.

again, you need to get knowledge of the real timing requirement

then you could choose to split the thing,
like in one draw you do the inner 3 for loops
and change
i++
if ( i >= cols ) i = 0;

for the next draw loop,
so you need cols run of draw to solve the thing one time

something like that possible?

1 Like

Oooh, yes, that should work, thank you!

Just in case anyone happens to get same error, one quick way to fix it for many cases is to place frameRate() at the end of the setup().

1 Like

thank you very much for feedback and try to help the community,
but that i not understand,
my thinking is that the

frameRate(120);

should be in setup();
do you want to say that it does matter in what line of setup() it is?

or you had it inside draw() but moved it back to setup()?

Setting the frame rate within setup() is recommended

Aparently yes! I didn’t have much faith in this solution but worked!

all i can think of is that

size(200,200);

should be first line
insofar the

frameRate(120);

must be after it, more i not know about that.

could you provide a working example ( of that problem ONLY ) please?

I wrote a sketch that needs to load a folder full of images into memory to be used in the animation. This takes some time - about 10-15seconds or even more. I was running into the same problem “Waited 5000ms for” and crash on starting the sketch. Completely commenting out or simply removing frameRate() from the setup got rid of the issue. I havent tried to put it at the end of setup() or at least after loading the images, but it would make sense that this is the culprit of such error in case of processes taking longer then 5s in the setup().

1 Like

Does moving your long loading to the first frame act as a workaround? Something like:

void draw(){
  if(frameCount==1) setup1();
}
void setup1() {
}

I just tested putting framerate at the end of setup() and this works too:

void setup() {

  // background colour
  background(0);

  noCursor();

  // start oscP5, listening for incoming messages at port 12000 
  oscP5 = new OscP5(this,12000);
  oscP5.plug(this,"ctlin","/ctlin"); // to be converted for PD OSC input

  // get all textures into an image pool
  println("\n\n~~~ loading textures into image pool ...\n");
  imgPool = getImages("/images/"); // a function parsing a folder
 
  // framerate
  frameRate(60);  
}

If I put frameRate(60); before the getImages() function (which takes some time to load), I get the error:

java.lang.RuntimeException: Waited 5000ms for: <20366593, 2ef18fe5>[count 2, qsz 0, owner <main-FPSAWTAnimator#00-Timer0>] - <main-FPSAWTAnimator#00-Timer0-FPSAWTAnimator#00-Timer1>
	at processing.opengl.PSurfaceJOGL$2.run(PSurfaceJOGL.java:410)
	at java.lang.Thread.run(Thread.java:748)
RuntimeException: Waited 5000ms for: <20366593, 2ef18fe5>[count 2, qsz 0, owner <main-FPSAWTAnimator#00-Timer0>] - <main-FPSAWTAnimator#00-Timer0-FPSAWTAnimator#00-Timer1>

So this solves it.

1 Like

Thank you for your comment Kll! Placing frameRate() at the end of setup() fixed my error! :slight_smile: