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 & HelpSyntax Questions › question about optimizing and speed of processing
Page Index Toggle Pages: 1
question about optimizing and speed of processing (Read 603 times)
question about optimizing and speed of processing
Jun 1st, 2009, 2:02pm
 
Hello,  im working on a project and i need to create a lot of objects, the problem im having is that when i create more than 200 object my processing sketches become slow, what about if i need to create more objects?
I need to create horizontal lines, each line is made of 10 objects and each object is made of 20 little lines. If i have 10 horizontal lines that means that i would have 100 objects and in each object i would have 20 iterations so if i need to make any operation or animation that means that i will have 2000 iterations each frame and that is very slow in my computer. Is there any way of optimizing or any trick for make it faster? Ive read that in c++ there are techniques such as binning and sorting , is it possible to make something like this in processing? any hint?

thanks in advance

Seb.

Re: question about optimizing and speed of processing
Reply #1 - Jun 1st, 2009, 2:39pm
 
The idea behind that is that you will have first to sort the array or the vector of your objects and then check every particle you have created checks just only for the one close to it...

hope I make some sence here...

I would like to give an example but I'm not sure if I can find something now and it really depends on what you want to do
You can think for example an algorithm like that:

//pseudocode
1.sort  the particles according to their x position
2.now  particle number 0 checks for number 1
3.particle number 1 checks for number 2
4.particle number 3 checks for number 4
and so on....


good luck with it
Re: question about optimizing and speed of processing
Reply #2 - Jun 1st, 2009, 3:37pm
 
Any chance of seeing this code?

There's the possibility we might see something you're doing that is a redundant operation that can be dropped.
Re: question about optimizing and speed of processing
Reply #3 - Jun 2nd, 2009, 1:23am
 
What  rabidmachine9 describes is called space partitioning, used in collision detection.
Actually, we don't even know if you have collisions or some other kind of complex algorithm. As st33d, unless making generic advices, we can't help without having more detail.

One of these generic advices is to avoid some pitfalls often seen in beginner's code, like using loadImage or createFont in the draw() routine.
Re: question about optimizing and speed of processing
Reply #4 - Jun 2nd, 2009, 4:13pm
 
Hello st33d, phylo and radidmache, here im posting the example code so you can better understand my problem. I have 8 horizontal lines made of little vertical lines , each vertical line is 1 px so for filling the screen (800, 500) im using 800 vertical lines , this means im making 8 * 800 iterations  at each frame and the animation runs slow . Is there any way to optimize this to make it run smooth?  
many thanks

here is the code:


Code:

import processing.opengl.*;

Line[] array_of_lines = new Line[8];

void setup( ) {
size(800, 500);
smooth();
for (int i=0 ; i < 8 ; i ++) {
array_of_lines[i] = new Line( i);
}
}

void draw( ) {
background(255,255,255);
//strokeCap(ROUND);
for (int i=0 ; i < 8 ; i ++) {
array_of_lines[i].draw_line();
}
// if (button_one == 1){
//interpolate ();
// }
}


class Line {
float move_x = 0;
int ii;
int regresa= 20;

Line (int o) {
ii = o;
}

void draw_line() {
move_x = move_x + 1;
pushMatrix();
translate(regresa - move_x , ii * 68);
for (int i=0 ; i < 800 ; i ++) {
float aumenta_interno ;
pushMatrix();
float variable_perlin = noise(i * 0.02 + ii);
scale(1, 2);
pushMatrix();
translate(0, 10);
scale(1, 0 + variable_perlin * 4);


translate(i , 0);
line(0 , 0 , 0 , 0 );
popMatrix();
popMatrix();


}
popMatrix();
}
}


Re: question about optimizing and speed of processing
Reply #5 - Jun 2nd, 2009, 10:59pm
 
You can simplify your drawing routine:
Code:
void draw_line()  {
move_x = move_x + 1;
pushMatrix();
translate(regresa - move_x , ii * 68);
for (int i=0 ; i < 800 ; i ++) {
pushMatrix();
float variable_perlin = noise(i * 0.02 + ii);
translate(0, 20);
scale(1, variable_perlin * 8);
translate(i , 0);
line(0, 0, 0, 0);
popMatrix();
}
popMatrix();
}

but I fear it won't make a big difference in speed! Wink
Anyway, you choose an interesting but indeed rather slow method of drawing.
A possible improvement is by using cache, a trick used by JavaFX to speed up drawing of immutable complex objects (if given the right hint).
The idea is to draw once your lines on a PImage (all in the same if they move at same rate, or each with its own otherwise) and draw the image instead.
Re: question about optimizing and speed of processing
Reply #6 - Jun 4th, 2009, 1:05am
 
philho, what about if i just draw one rectangle instead of a lot of vertical lines that have the same size? In that way i could avoid using so many iterations.  What do you think about this idea?
Page Index Toggle Pages: 1