# Getting really bad lag with a function

**URL:** <https://discourse.processing.org/t/getting-really-bad-lag-with-a-function/13725>\
**Category:** Coding Questions\
**Created:** [August 31, 2019, 2:58pm UTC](https://discourse.processing.org/t/getting-really-bad-lag-with-a-function/13725 "2019-08-31T14:58:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lax](https://avatars.discourse-cdn.com/v4/letter/l/b19c9b/32.png) [@lax](https://discourse.processing.org/u/lax)\
**Post date:** [August 31, 2019, 2:58pm UTC](https://discourse.processing.org/t/getting-really-bad-lag-with-a-function/13725/1 "2019-08-31T14:58:23Z")

</div>

Hi, I have this function which i run on every pixel, which causes the fps to drop to around 1 fps:

```auto
PVector kaleido(PVector uv)
{
  float th = atan2(uv.y, uv.x);
  float r = uv.magSq();
  th = sin(th * 6.283f / 0.89759790102f);
  return new PVector(cos(th) * r * .1, sin(th) * r * .1);
}

```

If anyone could suggest an alternative or help in any way it would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 31, 2019, 3:31pm UTC](https://discourse.processing.org/t/getting-really-bad-lag-with-a-function/13725/2 "2019-08-31T15:31:39Z")

</div>

> [@lax](#):
>
> If anyone could suggest an alternative or help in any way…

Dunno how much it’d help, but I guess it’s worth a try: 😶

```java
static final PVector kaleido(final PVector uv, final PVector res) {
  final float r = uv.magSq() * .1, th = sin(7 * uv.heading());
  return res.set(cos(th) * r, sin(th) * r);
}

```

> [@lax](#):
>
> Hi, I have this function which i run on every pixel, …

Knowing how you’re iterating over `pixels[]` would help too. 🤓

---

<div class="post-metadata">

**Author:** ![lax](https://avatars.discourse-cdn.com/v4/letter/l/b19c9b/32.png) [@lax](https://discourse.processing.org/u/lax)\
**Post date:** [August 31, 2019, 3:46pm UTC](https://discourse.processing.org/t/getting-really-bad-lag-with-a-function/13725/3 "2019-08-31T15:46:31Z")

</div>

Thanks for the reply. Changing to your function didn’t seem to help. I am looping over the pixels with two nested for loops. It lags when just calling the function in the for loop and nothing else.

```auto
void setup() {
  size(1000,920, P2D);
  frameRate(60);
  noStroke();
  noSmooth();
}
static PVector ret = new PVector(0,0,0);
static final PVector kaleido(PVector uv) {
  final float r = uv.magSq() * .1, th = sin(7 * uv.heading());
  return ret.set(cos(th) * r, sin(th) * r);
}

int frames = 0;

void draw() {
  frames++;
 for (int x = 0; x < width; x++) {
  for (int y = 0; y < height; y++) {
    kaleido(new PVector(1,1,1));
  }
 }
 text(frames, 10, 30); 
}

```

---

<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:** [August 31, 2019, 4:17pm UTC](https://discourse.processing.org/t/getting-really-bad-lag-with-a-function/13725/4 "2019-08-31T16:17:00Z")

</div>

What does the function `kaleido` do? At the moment it does nothing because you are passing the same information to it 920000 times a frame, no wonder it’s slow you are creating 982000 PVector objects every frame.

---

<div class="post-metadata">

**Author:** ![lax](https://avatars.discourse-cdn.com/v4/letter/l/b19c9b/32.png) [@lax](https://discourse.processing.org/u/lax)\
**Post date:** [August 31, 2019, 6:24pm UTC](https://discourse.processing.org/t/getting-really-bad-lag-with-a-function/13725/5 "2019-08-31T18:24:33Z")

</div>

Thanks for the help everyone, but i got it working fine by converting the code to a shader. It is code to produce a kaleidoscope effect :).
