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.
Page Index Toggle Pages: 1
Performance (Read 248 times)
Performance
Nov 13th, 2008, 11:54pm
 
HI!
I'm quite new with processing, but althought my work doesn't seem that complex compared to other stuff i've seen (flight404 good example) it has serious problems with framerate. That's the code:

import processing.opengl.*;



class circle {
 float x,y;
 float speedX,speedY;
 float speedX1,speedY1;
 float SX1;
 float SY1;
 float mX,mY;



 void init(){
   x = random (0,width);
   y = random (0,height);
   speedX=0;
   speedY=0;

   speedX1=0;
   speedY1=0;
 }

 void update(){


   x+= (speedX-speedX1);
   y+= (speedY-speedY1);
 
    if (mousePressed == true) {
    x+=speedX-speedX1+mX;
    y+=speedY-speedY1+mY;

   }    
 }


 void render(){
  /* noStroke();
   fill(10,81,95);
   ellipse (x,y,10,10);
   */

  float k=10;
  stroke(0,92,90);
  strokeWeight(3);
  line (x-k,y,x+k,y);
  line (x,y-k,x,y+k);
 }
}

class attrattori{
 float x,y;

 void render (){
   fill(10,81,95,100);
   ellipse(x,y,10,10);
 }
}

int nCircle =70;
circle [] cerchi = new circle [nCircle];

int nAtt =64;
int spWidth=16;
int spHeight=12;
attrattori [] punto  = new attrattori [nAtt];

void setup (){
 colorMode(HSB,100);
 size (1400,700,OPENGL);

 for (int i=0;i<nCircle;i++){
   cerchi[i] = new circle();
 }
 for (int i=0;i<nCircle;i++){
   cerchi[i].init();
 }

 for (int i=0;i<nAtt;i++){
   punto[i] = new attrattori();
 }


 int i=0;
 for (int x=0;x<width;x+=width/spWidth){
   punto[i].x=x;
   punto[i].y=0;
   //println("A  i="+i+"  x="+x+" y="+0);
   if (i<nAtt-1){
     i++;
   }
 }
 for (int x=0;x<width;x+=width/spWidth){
   punto[i].x=x;
   punto[i].y=height;
   //println("A  i="+i+"  x="+x+" y="+height);
   if (i<nAtt-1){
     i++;
   }
 }

 for (int y=0;y<height;y+=height/spHeight){

   punto[i].y=y;
   punto[i].x=0;
   //println("B  i="+i+"  x="+0+" y="+y);
   if (i<nAtt-1){
     i++;
   }
 }
 for (int y=0;y<height;y+=height/spHeight){

   punto[i].y=y;
   punto[i].x=width;
   //println("B  i="+i+"  x="+width+" y="+y);
   if (i<nAtt-1){
     i++;
   }
 }
}

void draw (){
 noStroke();
 fill(23, 4, 90,90);
 rect(0,0,width,height);
 if (keyPressed){
   if (key=='c'){
     for (int i=0; i<nCircle;i++){
       cerchi[i].init();
     }
   }
 }
 for (int i=0; i<nCircle;i++){
   cerchi[i].update();
 }
 for (int i=0; i<nCircle;i++){
   cerchi[i].render();
 }

 for (int i=0; i<nAtt;i++){
  // punto[i].render();
 }

//cerchi e attrattori

 for (int i=0; i<nCircle;i++){
   for (int j=0; j<nAtt; j++){
     float dAttX = (cerchi[i].x-punto[j].x);
     float dAttY = (cerchi[i].y-punto[j].y);
     float dAtt  = dist (cerchi[i].x,cerchi[i].y,punto[j].x,punto[j].y);


   cerchi[i].speedX += ((100)/((dAtt*dAtt*dAttX)+0.0001))*100;
   cerchi[i].speedY += ((100)/((dAtt*dAtt*dAttY)+0.0001))*100;
     //println("i ="+i+", j ="+j);
   }
 }

//cerchi un con l'altro

 for (int i=0; i<nCircle;i++){
   for (int j=0; j<nCircle;j++){

     float dX = (cerchi[i].x-cerchi[j].x);
     float dY = (cerchi[i].y-cerchi[j].y);

     float dd  = dist (cerchi[i].x,cerchi[i].y,cerchi[j].x,cerchi[j].y);

  cerchi[i].speedX1 =dX*dd*dd*0.000000001;
  cerchi[i].speedY1 =dY*dd*dd*0.000000001;
     if (dd<400){
       stroke(0,0,0,dd);
       float sw=(1/(dd*dd))*5000;
       strokeWeight(sw);
       if (sw>100){
         strokeWeight(100);
         line (cerchi[i].x,cerchi[i].y,cerchi[j].x,cerchi[j].y);
         }
         else{
           line (cerchi[i].x,cerchi[i].y,cerchi[j].x,cerchi[j].y);
         }
   
       //println("i ="+i+", j ="+j);
     }
     if (dd<=410 && dd>=405){
       float rx =random(-2,2);
       float ry =random(-2,2);
       //noFill();
       float xRect=(cerchi[i].x+((cerchi[j].x-cerchi[i].x)/3));
       float yRect=(cerchi[i].y+((cerchi[j].y-cerchi[i].y)/3));
       
       stroke(0);
       
       int k=5;
       line (xRect-k,yRect,xRect+k,yRect);
       line (xRect,yRect-k,xRect,yRect+k);
       
       //rect(xRect+rx,yRect+ry,10,10);

     }
   }
 }

 //mouseinput
   for (int i=0; i<nCircle;i++){
 
float mmX = (mouseX-cerchi[i].x);
float mmY = (mouseY-cerchi[i].y);
float mm  = dist(mouseX,mouseY,cerchi[i].x,cerchi[i].y);
cerchi[i].mX= (mmX/(mm*mm))*1000;
cerchi[i].mY= (mmY/(mm*mm))*1000;


   }

 smooth();
}


I Know that if i get rid of the "smooth" and if i decrease the particles it gets already better but i wish i could get to a better performance.

Any suggestion?
thanks
lorenzo
Re: Performance
Reply #1 - Nov 14th, 2008, 11:07pm
 
it's doing quite a lot

<code>
 for (int i=0; i<nCircle;i++){
   for (int j=0; j<nCircle;j++){
</code>

where nCircle = 70 means 4900 times through that loop

also think about what happens when j = i, does it make sense to do this?

<code>
float dAttX = (cerchi[i].x-punto[j].x);
float dAttY = (cerchi[i].y-punto[j].y);
float dAtt  = dist (cerchi[i].x,cerchi[i].y,punto[j].x,punto[j].y);
</code>

you might be able to get away with only doing half of these comparisons - you work out the distance between point 3 with point 4 (for example) and later work out the distance between 4 and 3. which will be the same.
Re: Performance
Reply #2 - Nov 14th, 2008, 11:21pm
 
i changed nCircles to 10 and printed out all the coords

(lineA is the line within the "dd<400" condition,
lineB is the other one. indexes are i and j)

Code:

LineA[0][0]: 158.08415,47.811424,158.08415,47.811424
LineA[1][1]: 220.40291,280.42963,220.40291,280.42963
LineA[2][2]: 380.9574,540.06433,380.9574,540.06433
LineA[3][3]: 1001.6303,420.28998,1001.6303,420.28998
LineA[4][4]: 506.69843,374.4198,506.69843,374.4198
LineA[5][5]: 206.04227,102.92694,206.04227,102.92694
LineA[6][6]: 324.68802,515.3001,324.68802,515.3001
LineA[7][7]: 372.7746,536.14685,372.7746,536.14685
LineA[8][8]: 669.50446,167.508,669.50446,167.508
LineA[9][9]: 987.21924,217.91843,987.21924,217.91843


the above don't need drawing at all

and all of these lines you are drawing twice but only need to be drawn once

Code:

LineA[0][1]: 158.08415,47.811424,220.40291,280.42963
LineA[1][0]: 220.40291,280.42963,158.08415,47.811424

LineA[1][2]: 220.40291,280.42963,380.9574,540.06433
LineA[2][1]: 380.9574,540.06433,220.40291,280.42963

LineA[1][4]: 220.40291,280.42963,506.69843,374.4198
LineA[4][1]: 506.69843,374.4198,220.40291,280.42963

LineA[2][4]: 380.9574,540.06433,506.69843,374.4198
LineA[4][2]: 506.69843,374.4198,380.9574,540.06433

LineA[0][5]: 158.08415,47.811424,206.04227,102.92694
LineA[5][0]: 206.04227,102.92694,158.08415,47.811424

LineA[1][5]: 220.40291,280.42963,206.04227,102.92694
LineA[5][1]: 206.04227,102.92694,220.40291,280.42963

LineA[1][6]: 220.40291,280.42963,324.68802,515.3001
LineA[6][1]: 324.68802,515.3001,220.40291,280.42963

LineA[1][7]: 220.40291,280.42963,372.7746,536.14685
LineA[7][1]: 372.7746,536.14685,220.40291,280.42963

LineA[2][6]: 380.9574,540.06433,324.68802,515.3001
LineA[6][2]: 324.68802,515.3001,380.9574,540.06433

LineA[2][7]: 380.9574,540.06433,372.7746,536.14685
LineA[7][2]: 372.7746,536.14685,380.9574,540.06433

LineA[3][9]: 1001.6303,420.28998,987.21924,217.91843
LineA[9][3]: 987.21924,217.91843,1001.6303,420.28998

LineA[4][6]: 506.69843,374.4198,324.68802,515.3001
LineA[6][4]: 324.68802,515.3001,506.69843,374.4198

LineA[4][7]: 506.69843,374.4198,372.7746,536.14685
LineA[7][4]: 372.7746,536.14685,506.69843,374.4198

LineA[4][8]: 506.69843,374.4198,669.50446,167.508
LineA[8][4]: 669.50446,167.508,506.69843,374.4198

LineA[6][7]: 324.68802,515.3001,372.7746,536.14685
LineA[7][6]: 372.7746,536.14685,324.68802,515.3001

LineA[8][9]: 669.50446,167.508,987.21924,217.91843
LineA[9][8]: 987.21924,217.91843,669.50446,167.508


and then there are these that you have to draw all of

Code:

LineB[4][5]: 401.4797,283.92218,411.4797,283.92218
LineB[5][4]: 301.261,193.42456,311.261,193.42456
LineB[5][4]: 306.261,188.42456,306.261,198.42456
LineB[4][5]: 406.4797,278.92218,406.4797,288.92218
Page Index Toggle Pages: 1