# Can you help me with optimizing code

**URL:** <https://discourse.processing.org/t/can-you-help-me-with-optimizing-code/21141>\
**Category:** Coding Questions\
**Created:** [May 21, 2020, 6:01pm UTC](https://discourse.processing.org/t/can-you-help-me-with-optimizing-code/21141 "2020-05-21T18:01:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arsenii](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@Arsenii](https://discourse.processing.org/u/Arsenii)\
**Post date:** [May 21, 2020, 6:01pm UTC](https://discourse.processing.org/t/can-you-help-me-with-optimizing-code/21141/1 "2020-05-21T18:01:38Z")

</div>

I have a problem with understanding who to optimize my code.  
Main idea of program of creating a words with dots.

```auto
ArrayList<PVector> edgeVertices = new ArrayList<PVector>();
ArrayList<PVector> Adding = new ArrayList<PVector>();
char[] word = new char[5];
PFont font;
PShape shape;
PVector max;
String fontPath;
char c;
int border = 0;
int distance = 10;
int adding_length =1;
void setup() {
  size(900, 300);
  background(51);
  fontPath = "arial.ttf"; // Path to font file "AvenirNextLTPro-Demi.otf" Comfortaa-Light.ttf
  word[0] = 't';
  word[1] = 'r';
  word[2] = 'a';
  word[3] = 'i';
  word[4] = 'n';
  for (int i = 0; i < word.length; i++) {
    c = word[i];
    font = createFont(fontPath, 166, true);
    shape = font.getShape(c);
    for (int j = 0; j < shape.getVertexCount(); j++) {
      edgeVertices.add(shape.getVertex(j));
    }
    max = new PVector(edgeVertices.get(0).x, edgeVertices.get(0).y + 165);
    stroke(255);
    strokeWeight(8);
    for (int j = 0; j < edgeVertices.size(); j++) {
      for (int k = 0; k < edgeVertices.size(); k++) {
        float di = edgeVertices.get(j).y - edgeVertices.get(k).y;
        if (di < 0) di*=-1;
        if (edgeVertices.get(j).x == edgeVertices.get(k).x && di > distance) {
          Adding.add(new PVector (edgeVertices.get(j).x, (edgeVertices.get(j).y < edgeVertices.get(k).y?edgeVertices.get(j).y:edgeVertices.get(k).y) + di/2));
          ++adding_length;
        }
      }
    }
    for (int k = 0;k < Adding.size()-1; k++) {
      edgeVertices.add(Adding.get(k));
    }
    for (PVector v : edgeVertices) {
      point(v.x + 25 + border, v.y + 165 );
      if (max.x < v.x) {
        max.x = v.x;
      }
    }

    border += max.x + 20;
    for (int k = shape.getVertexCount()-1; k > 0; k--) {
      edgeVertices.remove(shape.getVertex(k));
    }
    Adding = new ArrayList<PVector>();
  }
}
void draw() {
}

```

---

<div class="post-metadata">

**Author:** ![Arsenii](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@Arsenii](https://discourse.processing.org/u/Arsenii)\
**Post date:** [May 21, 2020, 6:04pm UTC](https://discourse.processing.org/t/can-you-help-me-with-optimizing-code/21141/2 "2020-05-21T18:04:07Z")

</div>

This program to work needs an arial font

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [May 21, 2020, 6:08pm UTC](https://discourse.processing.org/t/can-you-help-me-with-optimizing-code/21141/3 "2020-05-21T18:08:11Z")

</div>

Hello,

> [@Arsenii](#):
>
> shape = font.getShape©;

Please format your code as a courtesy to the community:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)  
You can go back and edit it.

I am not looking at any long unformatted code these days.

Try to copy your code and paste into Processing to understand this request.

`:)`

---

<div class="post-metadata">

**Author:** ![Arsenii](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@Arsenii](https://discourse.processing.org/u/Arsenii)\
**Post date:** [May 21, 2020, 6:26pm UTC](https://discourse.processing.org/t/can-you-help-me-with-optimizing-code/21141/4 "2020-05-21T18:26:23Z")

</div>

Thank you for helping with reorganizing my code.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [May 21, 2020, 8:24pm UTC](https://discourse.processing.org/t/can-you-help-me-with-optimizing-code/21141/5 "2020-05-21T20:24:49Z")

</div>

This was running at close to 100% CPU!

I added a println() here and there:

`println(edgeVertices.size());`

`edgeVertices.size()` kept growing larger!

This tamed the beast:

```auto
for (int k = 0;k < Adding.size()-1; k++) 
  {
  //edgeVertices.add(Adding.get(k));
  }

```

That is all I can offer at the moment.

It’s a start!

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/7/71af9b8084c5744939e95291630c3f1d483ad426.png)

`:)`
