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 performance with PGraphics
Pages: 1 2 
Slow performance with PGraphics (Read 2880 times)
Slow performance with PGraphics
Aug 21st, 2009, 8:03am
 
Hi all, i've made a simple program to test some "glow effect"; but on my mac g4 i got really poor performance, about 6 fps for just 3 circles...

here's the code:
Code:

PGraphics[] circles;

void setup() {
 size(500, 500);
 frameRate(30);
 
 circles = new PGraphics[3];

 circles[0] = makeCircle(color(255, 0, 0));
 circles[1] = makeCircle(color(0, 255, 0));
 circles[2] = makeCircle(color(0, 0, 255));


}

void draw() {
 background(0);
 
 /* uncomment this to get slow
 circles[0] = makeCircle(color(255, 0, 0));
 circles[1] = makeCircle(color(0, 255, 0));
 circles[2] = makeCircle(color(0, 0, 255));
 */
 
 print("frame rate: " + frameRate + "\n");

 image(circles[0], random(0,500), random(0,500));
 image(circles[1], random(0,500), random(0,500));
 image(circles[2], random(0,500), random(0,500));

}

PGraphics makeCircle (color c) {
 PGraphics pg = createGraphics(100, 100, JAVA2D);

 pg.beginDraw();

 pg.noFill();
 pg.stroke(c);
 pg.strokeWeight(10);
 pg.ellipse(50,50,50,50);
 pg.loadPixels();

 pg.filter(BLUR,4);

 pg.strokeWeight(1);

 pg.smooth();
 pg.ellipse(50,50,50,50);

 pg.endDraw();

 return pg;
}


i've also tried to remove the blur filter, but the problem seems to be also creating the PGraphics in the draw loop ...

in this example i don't need it, so i can create the circles only in the setup function, but if (for example) i need to canhge the colors dinamically?

of course i can draw directly without using PGraphics, but then i dunno how to obtain the glow effect with an efficient method ...
Re: Slow performance with PGraphics
Reply #1 - Aug 21st, 2009, 8:32am
 
I think getting slow performance while trying to instanciate many PGraphics on each frame is normal.

You should better consider PGraphics as offscreen-buffers, which means, you instanciate it once, then you clear it and draw on it as much as you want.

Why don't you just pass to makeCircle() the reference to your circles[i] object, instead of creating a new PGraphics object?
Re: Slow performance with PGraphics
Reply #2 - Aug 21st, 2009, 9:27am
 
i got 30fps Smiley , but im sure antiplastik is right anyway.

Btw, you can use println instead of adding "\n");
Re: Slow performance with PGraphics
Reply #3 - Aug 21st, 2009, 9:31am
 
i've cleaned up the code so now i'm using only one instance of PGraphics,
but the performance still poor (8 fps)

Code:

PGraphics pg;


void setup() {
size(500, 500);
frameRate(30);

pg = createGraphics(100, 100, JAVA2D);
}

void draw() {
background(0);


print("frame rate: " + frameRate + "\n");

image(makeCircle(color(255, 0, 0)), random(0,500), random(0,500));
image(makeCircle(color(0, 255, 0)), random(0,500), random(0,500));
image(makeCircle(color(0, 0, 255)), random(0,500), random(0,500));

}

PGraphics makeCircle (color c) {
//PGraphics pg = createGraphics(100, 100, JAVA2D);

pg.beginDraw();
pg.background(0);
pg.noFill();
pg.stroke(c);
pg.strokeWeight(10);
pg.ellipse(50,50,50,50);
pg.loadPixels();

pg.filter(BLUR,4);

pg.strokeWeight(1);

pg.smooth();
pg.ellipse(50,50,50,50);

pg.endDraw();

return pg;
}


* UPDATE *
if i coment out the pg.filter(BLUR,4); things get better (20 fps),
this mean PGraphics in any way is slower than the dircet draw, but now the problem is the blur function...

but now with the removal of blur i don't have my glow effect, any suggestion?
Re: Slow performance with PGraphics
Reply #4 - Aug 21st, 2009, 11:06am
 
Cedric wrote on Aug 21st, 2009, 9:27am:
i got 30fps Smiley , but im sure antiplastik is right anyway.


really so probably is something related to my HW ...
Re: Slow performance with PGraphics
Reply #5 - Aug 21st, 2009, 12:34pm
 
i just saw that you had the framerate limited to 30. so i tested your first sketch again and now ive got 60fps what is the maximum.

using your "cleaned up" code i only get 23 btw....

Re: Slow performance with PGraphics
Reply #6 - Aug 21st, 2009, 1:04pm
 
I got 20 on my old computer, with the last code.
Note: you want to do pg.background(0, 0); to avoid blocky background (when two circles are overlaid). You can also drop the loadPixels, with no purpose.

I wondered why you re-create identical circles repeatedly, but it seems you want to change the colors dynamically as well, that's right?
Re: Slow performance with PGraphics
Reply #7 - Aug 22nd, 2009, 12:10pm
 
PhiLho  wrote on Aug 21st, 2009, 1:04pm:
I wondered why you re-create identical circles repeatedly, but it seems you want to change the colors dynamically as well, that's right

Yes, this is my "demo objective". thanks for the suggestions, loadpixels was here for a previous test i've made and forgotten to remove it, but i see that this have no impact on performance

Cedric wrote on Aug 21st, 2009, 12:34pm:
i just saw that you had the framerate limited to 30. so i tested your first sketch again and now ive got 60fps what is the maximum.

using your "cleaned up" code i only get 23 btw....


about this... so my second version is slowest now i'm totaly confused ...
Re: Slow performance with PGraphics
Reply #8 - Aug 22nd, 2009, 12:19pm
 
yes, second one seems to be much slower.
Re: Slow performance with PGraphics
Reply #9 - Aug 22nd, 2009, 12:55pm
 
Cedric wrote on Aug 22nd, 2009, 12:19pm:
yes, second one seems to be much slower.

but in the first version have you un-commented these 3 lines

Code:
  
/* uncomment this to get slow
 circles[0] = makeCircle(color(255, 0, 0));
 circles[1] = makeCircle(color(0, 255, 0));
 circles[2] = makeCircle(color(0, 0, 255));
 */


just to be sure, the right version to test is this:

Code:

PGraphics[] circles;

void setup() {
 size(500, 500);
 frameRate(30);
 
 circles = new PGraphics[3];

 circles[0] = makeCircle(color(255, 0, 0));
 circles[1] = makeCircle(color(0, 255, 0));
 circles[2] = makeCircle(color(0, 0, 255));


}

void draw() {
 background(0);
 
 /* comment the following 3 lines of code to get speediest */
 circles[0] = makeCircle(color(255, 0, 0));
 circles[1] = makeCircle(color(0, 255, 0));
 circles[2] = makeCircle(color(0, 0, 255));
 
 
 print("frame rate: " + frameRate + "\n");

 image(circles[0], random(0,500), random(0,500));
 image(circles[1], random(0,500), random(0,500));
 image(circles[2], random(0,500), random(0,500));

}

PGraphics makeCircle (color c) {
 PGraphics pg = createGraphics(100, 100, JAVA2D);

 pg.beginDraw();

 pg.noFill();
 pg.stroke(c);
 pg.strokeWeight(10);
 pg.ellipse(50,50,50,50);

 pg.filter(BLUR,4);

 pg.strokeWeight(1);

 pg.smooth();
 pg.ellipse(50,50,50,50);

 pg.endDraw();

 return pg;
}
Re: Slow performance with PGraphics
Reply #10 - Aug 22nd, 2009, 1:00pm
 
i tested your last code again and removed the framerate again. I get arround 30 fps with these 3 lines. And when removed i get 60+
Re: Slow performance with PGraphics
Reply #11 - Aug 22nd, 2009, 2:05pm
 
Cedric wrote on Aug 22nd, 2009, 1:00pm:
i tested your last code again and removed the framerate again. I get arround 30 fps with these 3 lines. And when removed i get 60+


ok thanks, so we are talking about a performance drop of 50% ... and i have only 3 circles, for sure there's something i'm doing wrong but i need some help to understand what  Smiley
Re: Slow performance with PGraphics
Reply #12 - Aug 22nd, 2009, 2:26pm
 
50% or more, cause processing is limited to 60fps...
but i cant figure out why it is either...
sorry
Re: Slow performance with PGraphics
Reply #13 - Aug 23rd, 2009, 12:55am
 
Not sure if you do something wrong, drawing with effects like blur (made with soft, not hardware accelerated) is time consuming... Maybe you can do a bunch of random circles offhand and use them as cached data. Or make only one image and color it before adding it to the sketch: colorization might be faster than blurring.
Re: Slow performance with PGraphics
Reply #14 - Aug 23rd, 2009, 3:44pm
 
PhiLho  wrote on Aug 23rd, 2009, 12:55am:
... Or make only one image and color it before adding it to the sketch: colorization might be faster than blurring.

this seems to be a great practical solution, but i've some problem with tint(); once i use a color i cannot change the others ... for example, the first color is red, then all the circles are red...

Code:

PGraphics pg;


void setup() {
 size(500, 500);
 frameRate(60);

 pg = makeCircle();
}

void draw() {
 background(0);

 print("frame rate: " + frameRate + "\n");

 tint(255, 0, 0);
 image(pg, 100, 100);
   
 tint(0, 255, 0);
 image(pg, 200, 200);

 tint(0, 0, 255);
 image(pg, random(0,500), random(0,500));

}

PGraphics makeCircle () {
 PGraphics pg = createGraphics(100, 100, JAVA2D);

 pg.beginDraw();
 pg.background(0, 0);
 pg.smooth();
 pg.noFill();
 pg.stroke(255);
 pg.strokeWeight(10);
 pg.ellipse(50,50,50,50);
 pg.filter(BLUR, 4);
 pg.strokeWeight(1);

 pg.ellipse(50,50,50,50);

 pg.endDraw();

 return pg;
}
Pages: 1 2