# Getting really bad lag with a function

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

``````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.

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

``````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);
}
``````

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

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.

``````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);
}
``````

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.

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 :).