# How do I optimize/make my script run better/let processing take more resources

**URL:** <https://discourse.processing.org/t/how-do-i-optimize-make-my-script-run-better-let-processing-take-more-resources/8948>\
**Category:** Coding Questions\
**Created:** [March 4, 2019, 10:22pm UTC](https://discourse.processing.org/t/how-do-i-optimize-make-my-script-run-better-let-processing-take-more-resources/8948 "2019-03-04T22:22:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![F53](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/f53/32/7118_2.png) [@F53](https://discourse.processing.org/u/F53)\
**Post date:** [March 4, 2019, 10:22pm UTC](https://discourse.processing.org/t/how-do-i-optimize-make-my-script-run-better-let-processing-take-more-resources/8948/1 "2019-03-04T22:22:48Z")

</div>

I recently coded a simple point cloud script but it starts to lag really bad when I go above 250 or so points.  
Is there any way to optimize my script / let processing take more resources?  
keep in mind I am new to Processing, but know java really well.

here is a simplified version of my script without a few features or the settings menu:

```auto
float MaxSpeed = 2;
float MaxDBPts = 200; //max distance between points
float SizeFactor = 100;
float MaxSize = 5;
float windowX = 1920;
float windowY = 1080;
int pointAmount = 200;
point[] pList = new point[pointAmount];
void setup() {
  background(0);
  fullScreen();
  frameRate(60);
  stroke(255);
  for (int i = 0; i < pList.length; i++) {
    pList[i] = new point(random(windowX), random(windowY), random(-MaxSpeed, MaxSpeed), random(-MaxSpeed, MaxSpeed));
  }
}

class point {
  private float x, y, mx, my;
  point (float a, float b, float c, float d)
  {
    x = a;
    y = b;
    mx = c;
    my = d;
  }
  float getX() {
    return x;
  }
  float getY() {
    return y;
  }
  void cyclepoint()
  {
    x += mx;
    y += my;
    if (x > windowX) {
      x = 0;
    }
    if (y > windowY) {
      y = 0;
    }
    if (x < 0) {
      x = windowX;
    }
    if (y < 0) {
      y = windowY;
    }
  }
}

void draw() {
  background(0);
  float tempD = 0;
  point TP1;
  point TP2;

  for (int i = 0; i < pList.length; i++) {
    pList[i].cyclepoint();
  }
  for (int o = 0; o < pList.length; o++) {
    for (int t = 0; t < pList.length; t++) {
      TP1=pList[o];
      TP2=pList[t];//temporary point objects used to make tempd and other calculations easier to write and read
      tempD = sqrt(sq(TP1.getX() - TP2.getX()) + sq(TP1.getY() - TP2.getY()));//calculates distance between 2 points
      if (tempD<MaxDBPts&&tempD!=0) {
        if (SizeFactor/tempD<MaxSize) {//determines line thickness
          strokeWeight(SizeFactor/tempD);
        }
        else {
          strokeWeight(MaxSize);
        }
        line(TP1.getX(), TP1.getY(), TP2.getX(), TP2.getY());//draws the line
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 5, 2019, 12:18am UTC](https://discourse.processing.org/t/how-do-i-optimize-make-my-script-run-better-let-processing-take-more-resources/8948/2 "2019-03-05T00:18:01Z")

</div>

You got a nested for loop with o and t

You check ever pair twice

In the 2nd for loop say t=o+1

(In the first length-1)

Also sqrt and sq are very costly; google a better formula

---

<div class="post-metadata">

**Author:** ![hotfooted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hotfooted/32/3670_2.png) [@hotfooted](https://discourse.processing.org/u/hotfooted)\
**Post date:** [March 5, 2019, 12:56am UTC](https://discourse.processing.org/t/how-do-i-optimize-make-my-script-run-better-let-processing-take-more-resources/8948/3 "2019-03-05T00:56:56Z")

</div>

switch your renderer too.

```auto
fullScreen(P2D);

```
