We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Slow program
Page Index Toggle Pages: 1
Slow program (Read 397 times)
Slow program
Jun 28th, 2008, 11:57pm
 
Hello,
I made a very simple program which becomes very slow after a short while.
Basically it draws random noise (black pixels) into a PGraphics, renders it and scales it. At first it's pretty fast, but it becomes slower and slower.

Code:

class BombSquare {
int x,y;
int[][] sqrs;
PGraphics pg;
BombSquare() {
x = 580;
y = 580;
sqrs = new int[x][y];
for (int i=0;i<x;i++)
for (int j=0;j<y;j++)
sqrs[i][j] = 0;
pg = createGraphics(580, 580,JAVA2D);
pg.beginDraw();
}
void clear() {pg.background(255,0);}
void bomb(int times) {
for (int i=0;i<times;i++) {
int rx = (int)random(0,x), ry = (int)random(0,y);
sqrs[rx][ry] += 1;
}
}
PGraphics getPg() {
pg.background(255, 0);
for (int i=0;i<x;i++)
for (int j=0;j<y;j++)
if (sqrs[i][j]>0)
pg.point(i,j);
pg.endDraw();
return pg;
}
}
BombSquare bs = new BombSquare();
int x;
void setup() {
size(580,580);
x = 0;
}

void draw() {
background(255,0);
pushMatrix();
//it becomes slow even when no scale is used
//scale(1-0.001*x);
bs.bomb(100);
image(bs.getPg(),0,0);
popMatrix();
//if (x<700)
x++;
}

Re: Slow program
Reply #1 - Jun 29th, 2008, 12:01am
 
a slow change shows that the number of millis each draw cycle takes grows:
Code:

class BombSquare {
int x,y;
int[][] sqrs;
PGraphics pg;
BombSquare() {
x = 580;
y = 580;
sqrs = new int[x][y];
for (int i=0;i<x;i++)
for (int j=0;j<y;j++)
sqrs[i][j] = 0;
pg = createGraphics(580, 580,JAVA2D);
pg.beginDraw();
}
void clear() {pg.background(255,0);}
void bomb(int times) {
for (int i=0;i<times;i++) {
int rx = (int)random(0,x), ry = (int)random(0,y);
sqrs[rx][ry] += 1;
}
}
PGraphics getPg() {
pg.background(255, 0);
for (int i=0;i<x;i++)
for (int j=0;j<y;j++)
if (sqrs[i][j]>0)
pg.point(i,j);
pg.endDraw();
return pg;
}
}
BombSquare bs = new BombSquare();
int x;
void setup() {
size(580,580);
x = 0;
}
int ms = 0;
void draw() {
int now = millis();
println(now-ms);
ms = now;
background(255,0);
pushMatrix();
//scale(1-0.001*x);
bs.bomb(100);
image(bs.getPg(),0,0);
popMatrix();
//if (x<700)
x++;
}

Re: Slow program
Reply #2 - Jun 29th, 2008, 4:37am
 
I recently notices that even just feeling all pixels with random color is slow:
Code:

int x=580,y=580;
void setup() {
size(x,y);
noSmooth();
}
int ms = 0;
void draw() {
int tmp = millis();
println(tmp-ms);
ms = tmp;
for (int i=0;i<x;i++) {
for (int j=0;j<y;j++) {
stroke(random(0,256),random(0,256),random(0,256));
point(i,j);
}
}
}
Re: Slow program
Reply #3 - Jun 29th, 2008, 10:05am
 
Actually, using the point() method to fill the screen is slow. You should better write directly into the pixels array, that will speed up your loop :

Code:
loadPixels();
for (int i=0;i<x;i++) {
for (int j=0;j<y;j++) {
color c = color(random(0,256),random(0,256),random(0,256));
pixels[j*width+i] = c;
}
}
updatePixels();
Page Index Toggle Pages: 1