FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Programs
(Moderators: fry, REAS)
   how to optimize this very simple code?
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: how to optimize this very simple code?  (Read 1214 times)
lunetta

7200475272004752 WWW Email
how to optimize this very simple code?
« on: Apr 3rd, 2004, 7:04pm »

Hello to all P5 great community
 
I believe the true art of coding lies on the optimization; how to make your code elegant, sleek, neat, smooth?
Unhapply, it's not an art I'm a master.
 
I've done this very simple example code.
http://www.lunetta.com.br/circle/
The framerate is very, very low; why? My both computers (mac and pc) achieve much higher framerates with p5 applets that use 3D and complex algorithms... this one is very simple, but sluggish.
 
What's the path for higher framerates in this case?  
 
justo


Re: how to optimize this very simple code?
« Reply #1 on: Apr 3rd, 2004, 8:30pm »

it runs fairly quickly for me, but the problem in general is overdraw. since youre first filling a large circle, and then a slightly smaller one on top of that, and a slightly smaller one on top of that, over and over, you are actually re-setting individual pixels to different colors up to 20-some odd times.
 
i'm not exactly sure how youd could solve this in processing...translating in the z direction into the screen and drawing the circles small to large wouldnt draw over and over again, but you would still be calculating the circle's pixel coordinates and doing a z check, so it might not speed up hardly at all.
 
another way to do it might be to turn fill off and only draw thick outlines of circles, because thats essentially what youre doing...thick circular shells. but then you wouldnt be able to put them in arbitrary positions. if you dont want to be able to do this, see how this ends up looking.
 
the problem with circles is that setting a pixel isnt the only operation that goes on...when drawing you have to iterate around the circumference, filling as you go. so even if you did do a pixel check, you would still be doing most of the calculations. a shape like a rectangle you could cheat around with, but i dont think youre going to be able to get much of a speed increase out of circles.
 
lunetta

7200475272004752 WWW Email
Re: how to optimize this very simple code?
« Reply #2 on: Apr 3rd, 2004, 9:16pm »

Oops, so it's not as simple as it seems...
 
my initial approach was to use strokes instead of circles, BUT large weight strokes don't work on curves... try for yourself:
 
size(300,300);
ellipseMode(CENTER_RADIUS);
strokeWeight(20);
ellipse(150,150,100,100);
 
justo


Re: how to optimize this very simple code?
« Reply #3 on: Apr 3rd, 2004, 9:45pm »

oh, right, yeah, i forgot that that happens.
 
hmm, so you'll probably just have to wait until the guys that have actually seen the circle filling algorithm in the processing code get on the boards...they might have a few ideas.
 
Quasimondo

WWW Email
Re: how to optimize this very simple code?
« Reply #4 on: Apr 4th, 2004, 6:29pm »

What works best for me if I run into an optimization problem is to try a completely different approach. And I usually prefer direct pixel operations to workign via drawing commands. So here is a almost non-optimized approach that creates those circles on a pixel level by calculating the distance from the center. I added a little gimmick: mousey controls the speed and mousex the width of the strips.
 
Now I think some antialiasing could help to improve the appearance...
 
 
float c=0;
void setup() {
  size (300,300);
  noStroke();
   
}
 
void loop()
{
  int w2=width/2;
  int h2=height/2;
  int w1=width-1;
  int h1=height-1;
  c+=(mouseY-h2)/100.0;
  float w=(mouseX-w2)/5.0;
  for (int y=0;y<h2;y++){
    for (int x=0;x<w2;x++){
 int dx=w2-x;
 int dy=h2-y;
 int d=int((c+sqrt(float(dx*dx+dy*dy)))/w);
 if ((d&1)==1){
   pixels[x+y*width]= pixels[w1-x+y*width]=pixels[w1-x+(h1-y)*width]=pixels[x+(h1-y)*width]=0xff000000;
 } else {
   pixels[x+y*width]= pixels[w1-x+y*width]=pixels[w1-x+(h1-y)*width]=pixels[x+(h1-y)*width]=0xffffffff;  
 }
    }
  }
 
}
 

Mario Klingemann | Quasimondo | Incubator | côdeazur
lunetta

7200475272004752 WWW Email
Re: how to optimize this very simple code?
« Reply #5 on: Apr 4th, 2004, 7:29pm »

You're really right; I should have thought about this approach. I guess I'm too attached do vector-drawing-code yet... Thanks for the help!
 
Quasimondo

WWW Email
Re: how to optimize this very simple code?
« Reply #6 on: Apr 4th, 2004, 7:52pm »

And based on that code, here is a hypnosis spiral:
 
http://incubator.quasimondo.com/processing/hypnosis.php
 
Sourcecode:
http://incubator.quasimondo.com/processing/hypnosis.pde
 

Mario Klingemann | Quasimondo | Incubator | côdeazur
lunetta

7200475272004752 WWW Email
Re: how to optimize this very simple code?
« Reply #7 on: Apr 4th, 2004, 8:07pm »

That's just fantastic! Now I'm seeing psychedelic patterns everywhere. I hope the damage isn't permanent
 
fry


WWW
Re: how to optimize this very simple code?
« Reply #8 on: Apr 4th, 2004, 8:50pm »

quasimondo's method is solid.. once p5 is further along and we get the strokeWeight() problems sorted out, that will be the proper method to do it--just do draw a set of concentric stroked ellipses without a fill, because of the problems justo cited..
 
Pages: 1 

« Previous topic | Next topic »