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 › Why is this so slow
Page Index Toggle Pages: 1
Why is this so slow? (Read 2148 times)
Why is this so slow?
May 5th, 2005, 8:57am
 
hey guys,

i've tried to move 1000 ellipses at random movement. and when i play it, its very choppy. but when i used same code (only using loop instead of draw) with Alpha 0069, its really fast. why is this?

here's my sample code:

Code:



Pixel[] pixel = new Pixel[1000];

void setup(){
size(300,300);
background(0);
ellipseMode(CENTER);
noStroke();

makePixel();
}

void makePixel(){

for(int i=0; i<pixel.length; i++){
pixel[i] = new Pixel(random(width),random(height));
}
}

void draw(){
background(0);

for(int i=0; i<pixel.length; i++){
pixel[i].move();
}

}

class Pixel{
float origX,origY;
float newX,newY;
color c;

Pixel(float x, float y){
origX = x;
origY = y;
c = color(random(255),random(255),random(255),100);
}

void move(){
newX = random(-10,10) + origX;
newY = random(-10,10) + origY;

fill(c);
ellipse(newX,newY,10,10);
}
}




thanks you guys!

Jon
Re: Why is this so slow?
Reply #1 - May 5th, 2005, 9:22am
 
Try changing the size(300,300); line to size(300,300,P3D);
Re: Why is this so slow?
Reply #2 - May 5th, 2005, 10:41am
 
nice! thanks a bunch man!

question: why P3D?
Re: Why is this so slow?
Reply #3 - May 5th, 2005, 12:42pm
 
The default is JAVA2D, which is unfortunately not as quick as P3D.

I'm not clued up on why JAVA2D is slower, but I think it probably provides features that P3D doesn't (smooth() for instance), and hence is the default.
Re: Why is this so slow?
Reply #4 - May 5th, 2005, 3:56pm
 
you might also try setting the framerate(). it seems there's a threading problem with java2d that makes it seem choppy and slow even though it's running quite quickly.
Re: Why is this so slow?
Reply #5 - May 6th, 2005, 10:03am
 
no luck with framerate also. sorry it got slower when i using ellipse rather than a rectangle.

tested it on the Alpha 0069 and still runs faster.

again here's my new code using Beta:
Code:

// Pixel Swarm
// Jon Danao
// 2005.05.05
// www.danao.org

// init values
PImage img;
int dim = 500;

Pixel[] pixel = new Pixel[10000];
int   pixelSize = 7;
color pixelColor;
int   pixelCount = 0;


void setup(){
 size(dim,dim,P3D);
 background(255);
 
 noStroke();
 ellipseMode(CENTER);
 loadPixelData();
}

void loadPixelData(){

 img = loadImage("picture.jpg");
 image(img,0,0);
 
 for(int i=0; i<dim; i+=3){
   for(int j=0; j<dim; j+=3){
     pixelColor = get(i,j);
     
     if(pixelColor != color(255,255,255)){
       pixel[pixelCount] = new Pixel(i,j,pixelColor);
       pixelCount++;
     }
   }
 }
 
 println(pixelCount);
}

class Pixel{
 float x0,y0;
 float x1,y1;
 float xd,yd;
 float xs,ys;
 boolean exceeds = true;
 color c;
 
 Pixel(float i, float j, color pixelColor){
   x0 = i;
   y0 = j;
   c = pixelColor;
   
   x1 = x0;
   y1 = y0;
 }
 
 void move(){
   if(exceeds){
     xd = random(-15,15) + x0;
     yd = random(-15,15) + y0;
     
     xs = (xd - x1) * 0.09;
     ys = (yd - y1) * 0.09;
     
     exceeds = false;
   }
   
   x1 += xs;
   y1 += ys;
   
   fill(c);
   ellipse(x1,y1,pixelSize,pixelSize);
   
   if(x1 > xd || y1 > yd){
     exceeds = true;
   }
 }
}


void draw(){
 background(255);

 for(int i=0; i<pixelCount; i++){
   pixel[i].move();
 }
}

same problem -- v69 vs. 87
Reply #6 - May 6th, 2005, 4:12pm
 
I also noticed the same problem in taking this
code and comparing it to version 69 - where it
runs so quickly that you can't detect a frame
change. With 87, it is much slower regardless of
using P3D or OPENGL. I also tried turning off the
hardware acceleration, but this didn't change
anything.

Re: Why is this so slow?
Reply #7 - May 6th, 2005, 4:37pm
 
Can you toss up a link to your sample image?
Re: Why is this so slow?
Reply #8 - May 7th, 2005, 6:33pm
 
maybe some more infos upon this topic here: http://processing.org/discourse/yabb_beta/YaBB.cgi?board=SoftwareBugs;action=display;num=1114119953
Re: Why is this so slow?
Reply #9 - May 9th, 2005, 10:04pm
 
Just as a followup, I put in some code that prints
frame rate per second, and compared V69 with V89.
a specific framerate() was set to 20. I am getting
roughly 13fps on V69. For V89, here are some
average rates:

(1) default:  3.8
(2) P3D:      2.4
(3) OpenGL:   1.3

Framerate was set to specific values, and this didn't
change anything substantially. I settled on 20.
Here is the code (pick a .jpg image that you have
to test it):


import processing.opengl.*;


// Pixel Swarm
// Jon Danao
// 2005.05.05
// www.danao.org

// fps test
float fps;
int last_time,elapsed_time;

// init values
PImage img;
int dim = 300;

Pixel[] pixel = new Pixel[10000];
int   pixelSize = 7;
color pixelColor;
int   pixelCount = 0;


void setup(){
 size(dim,dim);
 framerate(20);
 last_time = 0;
 background(255);
 
 noStroke();
 ellipseMode(CENTER_RADIUS);
 loadPixelData();
}

void loadPixelData(){

 img = loadImage("water.jpg");
 image(img,0,0);
 
 for(int i=0; i<dim; i+=3){
   for(int j=0; j<dim; j+=3){
     pixelColor = get(i,j);
     
     if(pixelColor != color(255,255,255)){
       pixel[pixelCount] = new Pixel(i,j,pixelColor);
       pixelCount++;
     }
   }
 }
 
 println(pixelCount);
}

class Pixel{
 float x0,y0;
 float x1,y1;
 float xd,yd;
 float xs,ys;
 boolean exceeds = true;
 color c;
 
 Pixel(float i, float j, color pixelColor){
   x0 = i;
   y0 = j;
   c = pixelColor;
   
   x1 = x0;
   y1 = y0;
 }
 
 void move(){
   if(exceeds){
     xd = random(-15,15) + x0;
     yd = random(-15,15) + y0;
     
     xs = (xd - x1) * 0.09;
     ys = (yd - y1) * 0.09;
     
     exceeds = false;
   }
   
   x1 += xs;
   y1 += ys;
   
   fill(c);
   ellipse(x1,y1,pixelSize,pixelSize);
   
   if(x1 > xd || y1 > yd){
     exceeds = true;
   }
 }
}


void draw(){
 elapsed_time = millis();
 fps = 1000.0/(float) (elapsed_time - last_time);
 last_time = elapsed_time;
 println(fps);
 
 background(255);
//   for (int i=0;i<2;i++) {
 for(int i=0; i<pixelCount; i++){
   pixel[i].move();
 }
}  



Page Index Toggle Pages: 1