# "Waited 5000ms for" issue

**URL:** <https://discourse.processing.org/t/waited-5000ms-for-issue/10830>\
**Category:** Coding Questions\
**Created:** [May 2, 2019, 7:55am UTC](https://discourse.processing.org/t/waited-5000ms-for-issue/10830 "2019-05-02T07:55:30Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![rogerotein](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rogerotein/32/4943_2.png) [@rogerotein](https://discourse.processing.org/u/rogerotein)\
**Post date:** [May 2, 2019, 7:55am UTC](https://discourse.processing.org/t/waited-5000ms-for-issue/10830/1 "2019-05-02T07:55:30Z")

</div>

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.

```auto
      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;
        }
      }

```

 ![javaw_2019-05-02_09-49-24](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/7/77f7d6b304bd9f0c2f9547343292a272c178e91d.png)

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.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [May 2, 2019, 8:51am UTC](https://discourse.processing.org/t/waited-5000ms-for-issue/10830/2 "2019-05-02T08:51:04Z")

</div>

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,

```auto
coord_.y

```

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

* * *

the

```auto
float d  

```

inside the for loop

* * *

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

---

<div class="post-metadata">

**Author:** ![rogerotein](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rogerotein/32/4943_2.png) [@rogerotein](https://discourse.processing.org/u/rogerotein)\
**Post date:** [May 2, 2019, 9:06am UTC](https://discourse.processing.org/t/waited-5000ms-for-issue/10830/3 "2019-05-02T09:06:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [May 2, 2019, 9:16am UTC](https://discourse.processing.org/t/waited-5000ms-for-issue/10830/4 "2019-05-02T09:16:21Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rogerotein](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rogerotein/32/4943_2.png) [@rogerotein](https://discourse.processing.org/u/rogerotein)\
**Post date:** [May 2, 2019, 1:08pm UTC](https://discourse.processing.org/t/waited-5000ms-for-issue/10830/5 "2019-05-02T13:08:25Z")

</div>

Oooh, yes, that should work, thank you!

---

<div class="post-metadata">

**Author:** ![rogerotein](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rogerotein/32/4943_2.png) [@rogerotein](https://discourse.processing.org/u/rogerotein)\
**Post date:** [May 3, 2019, 7:09pm UTC](https://discourse.processing.org/t/waited-5000ms-for-issue/10830/6 "2019-05-03T19:09:43Z")

</div>

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().

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [May 4, 2019, 12:22am UTC](https://discourse.processing.org/t/waited-5000ms-for-issue/10830/7 "2019-05-04T00:22:04Z")

</div>

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()?

> **[frameRate() / Reference](https://processing.org/reference/frameRate_.html)**
>
> Specifies the number of frames to be displayed every second. For example, the function call frameRate(30) will attempt to refresh 30 times a second. If the processor is not fast enough to maint…

> Setting the frame rate within **setup()** is recommended

---

<div class="post-metadata">

**Author:** ![rogerotein](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rogerotein/32/4943_2.png) [@rogerotein](https://discourse.processing.org/u/rogerotein)\
**Post date:** [May 4, 2019, 12:41am UTC](https://discourse.processing.org/t/waited-5000ms-for-issue/10830/8 "2019-05-04T00:41:11Z")

</div>

> [@kll](#):
>
> do you want to say that it does matter in what line of setup() it is?

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

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [May 4, 2019, 12:49am UTC](https://discourse.processing.org/t/waited-5000ms-for-issue/10830/9 "2019-05-04T00:49:53Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Luka](https://avatars.discourse-cdn.com/v4/letter/l/a587f6/32.png) [@Luka](https://discourse.processing.org/u/Luka)\
**Post date:** [April 22, 2020, 4:39pm UTC](https://discourse.processing.org/t/waited-5000ms-for-issue/10830/10 "2020-04-22T16:39:00Z")

</div>

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().

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [April 22, 2020, 5:10pm UTC](https://discourse.processing.org/t/waited-5000ms-for-issue/10830/11 "2020-04-22T17:10:03Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Luka](https://avatars.discourse-cdn.com/v4/letter/l/a587f6/32.png) [@Luka](https://discourse.processing.org/u/Luka)\
**Post date:** [April 22, 2020, 5:27pm UTC](https://discourse.processing.org/t/waited-5000ms-for-issue/10830/12 "2020-04-22T17:27:12Z")

</div>

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

```auto
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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![JSGauthier](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jsgauthier/32/9910_2.png) [@JSGauthier](https://discourse.processing.org/u/JSGauthier)\
**Post date:** [May 13, 2020, 3:32am UTC](https://discourse.processing.org/t/waited-5000ms-for-issue/10830/13 "2020-05-13T03:32:00Z")

</div>

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