Running really slow for a relatively simple program

I’ve made much more complicated programs than this yet its running at 30fps when at low resolution. If I go fullscreen it drops to less than ten fps. I think I must be missing something really obvious that is causing this massive slowdown but I can’t see what it is. Any help would be greatly appreciated.

float[] randomV;
int density = 200;
int lowB = 0;
int change = 1;
float hue = 0;
  float bri = 0;
  float r = 0;

void setup(){
size(500,500);
//fullScreen(P2D);
 background(0);
 //noLoop();
 noFill();
 strokeWeight(3);
 randomV = new float[density];
 generateRandomV();
 
 colorMode(HSB,360,100,100);
}

void draw(){
  background(0);
  translate(450,250);
  for(int i=density-1; i>0; i--){
    hue = map(i,0,density,lowB,lowB+80);
    bri = map(i,density,0,100,600);
    stroke((hue+(randomV[i]*10))%360,100,bri,50);
    r = randomV[i];
   ellipse(-i-r,0,10+((i+r)*2),10+((i+r)*2)); 
  }
  if(lowB==280){
   change = -1;
  }else if(lowB==0){
   change = 1; 
  }
  if(frameCount%2==0){
  lowB+=change;
  }

  
}

void generateRandomV(){
  for(int i = 0; i<density; i++){
  randomV[i] = random(20); 
 }
}

void mousePressed(){
 generateRandomV(); 
}
1 Like

Hello,

It rocks with:
size(1000, 1000, FX2D);

and

size(1000, 1000, P3D);

P3D rapidly declined as density increased.

I added this to monitor frameRate:

  if (frameCount%5 == 0)
    surface.setTitle(str(frameRate));

https://processing.org/reference/setTitle_.html

There is a comment about FX2D here:
https://processing.org/reference/environment/

:)

2 Likes

Hello,

Another approach is to create all of your shapes in advance.

Reference:
https://processing.org/tutorials/pshape/

In setup():

  for (int i=density-1; i>0; i-=1) 
    {
    //hue = map(i, 0, density, lowB, lowB+80);
    //bri = map(i, density, 0, 100, 600);
    //stroke((hue+(randomV[i]*10))%360, 100, bri, 50);
    r = randomV[i];
    float r1 = i+r;
    float r2 = 10+(r1*2);
    shapes[i] = createShape(ELLIPSE, -r1, 0, r2, r2);
    }

Use the same loop in draw() but color and display shape the shape:

    color c = color((hue+(randomV[i]*10))%360, 100, bri, 50);
    shapes[i].setStroke(c);
    shape(shapes[i], 0, 0);

https://processing.org/tutorials/pshape/
https://processing.org/reference/PShape_setStroke_.html

With density of 1000 I got 60 fps with P2D, P3D, FX2D but slow with default renderer.

:)

1 Like

Thank you so much for your help. I’d not heard of FX2D. I’ve never encountered this level of slowdown with P2D. I assumed that since I was drawing 2D shapes I should use a 2D renderer so didn’t even think of P3D. Kind of strange. Probably above my level of expertise to understand why this is.
Thanks for the detailed response :smiley:

2 Likes

It is ellipse. I’m not sure why, but switch to rect 58-50 fps… back to ellipse, 17fps.