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