# Create 500 PGraphics at the same time

**URL:** https://discourse.processing.org/t/create-500-pgraphics-at-the-same-time/36900
**Category:** Coding Questions
**Created:** [May 15, 2022, 2:53pm UTC](https://discourse.processing.org/t/create-500-pgraphics-at-the-same-time/36900 "2022-05-15T14:53:11Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Rag](https://avatars.discourse-cdn.com/v4/letter/r/b19c9b/32.png) [@Rag](https://discourse.processing.org/u/Rag)
#### Post date: [May 15, 2022, 2:53pm UTC](https://discourse.processing.org/t/create-500-pgraphics-at-the-same-time/36900/1 "2022-05-15T14:53:11Z")

</div>

I am trying to optimize a part of my code that creates 500 PGraphics. Is there a way to creates them all at the same time ?

```auto
final int child_count = 5;
final int min_size = 5;
final int max_size = 300;
final int position_variation = 35;
final int size_variation = 20;
final int rotation_variation = 10;
final int alpha_variation = 30;
PImage img;

void setup() {
  img = loadImage("imge.png");
}

void draw() {
  Shape[] survivors = {}; // Assume that there are 100 objects in this variable
  Shape[] children = {};
  
  for(Shape shape: survivors) {
    for(int child=0; child < child_count; child++) {
      float x = randomVar(shape.x, position_variation, 0, img.width);
      float y = randomVar(shape.y, position_variation, 0, img.height);
      float sizeX = randomVar(shape.sizeX, size_variation, min_size, max_size);
      float sizeY = randomVar(shape.sizeY, size_variation, min_size, max_size);
      float rotation = randomVar(shape.rotation, rotation_variation, -180, 180);
      float alpha = randomVar(alpha(shape.c), alpha_variation, 0, 255);
      children = (Shape[]) append(children, new Shape(x, y, sizeX, sizeY, rotation, alpha));
    }
  }
}

float randomVar(float original, float range, float min, float max) {
  return constrain(original + random(-range, range+1), min, max);
}

```

I tried to use a `thread()` that fills up `children` and a `while` to wait for it to be full, but I didn’t came up to anything working, I am not an expert at all in threads…

How can I create a lot of PGraphics at the same time and put them inside an array ?

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [May 15, 2022, 3:04pm UTC](https://discourse.processing.org/t/create-500-pgraphics-at-the-same-time/36900/2 "2022-05-15T15:04:18Z")

</div>

The draw method is called ~60 times a second and is used to render the frames. It is not the place to create the shapes. You might trty the following although its difficult to test as we don’t have the Shape class code

```auto
final int child_count = 5;
final int min_size = 5;
final int max_size = 300;
final int position_variation = 35;
final int size_variation = 20;
final int rotation_variation = 10;
final int alpha_variation = 30;
PImage img;

Shape[] survivors = {}; // Assume that there are 100 objects in this variable
Shape[] children = {};

void setup() {
  img = loadImage("imge.png");
  for (Shape shape : survivors) {
    for (int child=0; child < child_count; child++) {
      float x = randomVar(shape.x, position_variation, 0, img.width);
      float y = randomVar(shape.y, position_variation, 0, img.height);
      float sizeX = randomVar(shape.sizeX, size_variation, min_size, max_size);
      float sizeY = randomVar(shape.sizeY, size_variation, min_size, max_size);
      float rotation = randomVar(shape.rotation, rotation_variation, -180, 180);
      float alpha = randomVar(alpha(shape.c), alpha_variation, 0, 255);
      children = (Shape[]) append(children, new Shape(x, y, sizeX, sizeY, rotation, alpha));
    }
  }
}

void draw() {
}

float randomVar(float original, float range, float min, float max) {
  return constrain(original + random(-range, range+1), min, max);
}

```

---

<div class="post-metadata">

### Author: ![Rag](https://avatars.discourse-cdn.com/v4/letter/r/b19c9b/32.png) [@Rag](https://discourse.processing.org/u/Rag)
#### Post date: [May 15, 2022, 4:10pm UTC](https://discourse.processing.org/t/create-500-pgraphics-at-the-same-time/36900/4 "2022-05-15T16:10:54Z")

</div>

The fact that it is in the `draw` method is not a problem, all I want is optimize the two `for` loops. The code I posted above is a lot shorter than my whole code.

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [May 15, 2022, 5:14pm UTC](https://discourse.processing.org/t/create-500-pgraphics-at-the-same-time/36900/5 "2022-05-15T17:14:32Z")

</div>

> [@Rag](#):
>
> all I want is optimize the two `for` loops

Why are you having a problem maintaining a decent frame rate? What frame rate do you want?

Based on the code you have posted it unlikely there will be any benefit from using multiple threads.

---

<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: [May 15, 2022, 5:41pm UTC](https://discourse.processing.org/t/create-500-pgraphics-at-the-same-time/36900/6 "2022-05-15T17:41:17Z")

</div>

> [@Rag](#):
>
> The fact that it is in the `draw` method is not a problem

It is a huge problem as has been pointed out.

new is costly and append as well.

Put it in setup() and when you really want to change it 60 times per second don’t use new but change the values of the existing objects.

---

<div class="post-metadata">

### Author: ![Rag](https://avatars.discourse-cdn.com/v4/letter/r/b19c9b/32.png) [@Rag](https://discourse.processing.org/u/Rag)
#### Post date: [May 15, 2022, 6:55pm UTC](https://discourse.processing.org/t/create-500-pgraphics-at-the-same-time/36900/7 "2022-05-15T18:55:11Z")

</div>

Sorry if I don’t know how to say it clearly… "-\_-  
This is only a small part of my code, the whole program takes about 10 seconds to calculate everything, and then starts over.  
I made a [github repository](https://github.com/Rag404/image_recreation/) if you want to understand what my program does
