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 › help improving applet performance
Page Index Toggle Pages: 1
help improving applet performance (Read 1122 times)
help improving applet performance
May 4th, 2009, 12:05pm
 
hi

I am starting to experience performance issues with my applet and I'm nowhere near adding all the forces of the fully implemented version - this one is just to get the behaviour right.

http://upsdn.ca/processing/hairplay02d/applet

It is a particle system - each point along the perimeter of a circle is connected to every other one, along the lengthg of this connection, new particles are added, connected by springs. I have other questions about this sketch but for now I can't do anythng until I sort out ways to make it run smoother...

thanks
Re: help improving applet performance
Reply #1 - May 6th, 2009, 10:24am
 
still need help. not sure how other applets with many times more particles cope - why are my applets so memory hungry????

I have made two new versions, one pre-brakdown, the other one that cripples my computer...help? please?


http://upsdn.ca/processing/hairplay02f/applet

http://upsdn.ca/processing/hairplay02g/applet
Re: help improving applet performance
Reply #2 - May 6th, 2009, 1:51pm
 
I haven't spent much time on it but a few things stand out.

Try to avoid declarations inside loops. This adds extra stuff that Java has to do.

You have several loops in the draw method. If you can (again, haven't looked to closely) try to do several things during the same loop:

i.e.
Code:

Particle p;//declare variable p
for (int i=0; i<largestArray.length; i++) {
p = aParticle;
if (i < arrayA.length) //do something with arrayA;
if (i < arrayB.length) //do something with arrayB;
...
}


If you know that nothing will be added/removed from an ArrayList , you can store the result of arrayList.size() into a variable. This will reduce the number of function calls.

Also, if you know the type and number of objects you are working with, you could simply use an array of that object. ArrayLists are faster than Vectors, but there is still an added function call to get().

Smooth() can slow things down too, using P2D or P3D may help.

A lot of objects means lots of memory usage. Every reference to an object requires 8 bytes by itself.
The internal array of an ArrayList will typically be larger than the number of objects that you have added which means they often take a little extra.
Re: help improving applet performance
Reply #3 - May 6th, 2009, 8:46pm
 
thanks alot. I'm going to try implementing your suggestions. That said, my gut tells me that they don't fully explain the problem...

Can you or some one else give me a sense of what may be a high number of particles for a processing applet? I feel like I'm way under that mark - the hairplay2g applet which shows the symptoms I have been concerned about probably only has around 60 particles...which seems miniscule compared to some other stuff I have seen out there...but maybe I'm wrong
Re: help improving applet performance
Reply #4 - May 7th, 2009, 9:02am
 
One more thing I found was the println("on") and println("off").
Printing to the console repeatedly can drag down a program.

Slowly looking...
Re: help improving applet performance
Reply #5 - May 7th, 2009, 9:53am
 
NoahBuddy wrote on May 6th, 2009, 1:51pm:
Try to avoid declarations inside loops. This adds extra stuff that Java has to do.

Excuse me but that's a bad advice...  Roll Eyes

For your defense, I thought the same for a long time!
But I have seen several articles against this style of programming.

Modern Java compiler just make the "optimization" pointless, and declaration isn't taking CPU time anyway, it just moves a heap pointer or something like that.
And putting the declaration inside the loop is better, because it reduces the visibility of the variable strictly to the needed scope.
Re: help improving applet performance
Reply #6 - May 7th, 2009, 10:49am
 
Shocked
Is that true for objects as well as primitives?
Re: help improving applet performance
Reply #7 - May 7th, 2009, 10:56am
 
i just got some advice that the libraries I am using are probably causing the problems. Sure enough, disabling the ability to adjust the particle parameters via the P5 controls sped things up, but has meant that I can't tweak the parameters...

assuming I find values that work, I should also find a faster particle system - I may not need something as fancy as traer's. Does anyone know of any others?
Re: help improving applet performance
Reply #8 - May 7th, 2009, 12:24pm
 
NoahBuddy wrote on May 7th, 2009, 10:49am:
Is that true for objects as well as primitives

Actually, your p variable is a kind of primitive, just a reference (a pointer in C talk) to the real object.

I just found back one of the articles I mentioned: Myth - Defining loop variables inside the loop is bad for performance.
Re: help improving applet performance
Reply #9 - May 7th, 2009, 12:35pm
 
I think I get it now.

This would be slow:
Code:

for (...) {
Object o = new Object(); //what I was thinking/remembering

//or even "o = new Object();" defined elsewhere
}

but this

Code:

for (...) {
Object o = anotherObject;
}

Is OK.
Re: help improving applet performance
Reply #10 - May 7th, 2009, 12:43pm
 
This is interesting. Takes longer even if the variable is defined outside the loop.

Code:

final int COUNTS = 20000000;
long mils=0;
Integer a = (int)random(512);
Integer g=10;

mils = System.currentTimeMillis();
for (int i=0; i<COUNTS; i++) {
g = 2*a;
}
println(System.currentTimeMillis() - mils);

mils = System.currentTimeMillis();
for (int i=0; i<COUNTS; i++) {
Integer f = 2*a;
}
println(System.currentTimeMillis() - mils);

Edit: By the way, changing 'Integer' to 'int' brings the times to about the same.
Re: help improving applet performance
Reply #11 - May 7th, 2009, 12:57pm
 
NoahBuddy wrote on May 7th, 2009, 12:43pm:
Takes longer even if the variable is defined outside the loop.

Yes, I recalled something like that but I wasn't sure enough to say that.

Quote:
This would be slow: Object o = new Object();

Not so much unless the object creation itself is costly.
Being in the inner scope, they are short lived and I believe they are in the young generation of the garbage collector so they are quickly collected.
Of course, if the object is constant through the whole loop, it is probably better to put it outside of the loop, though.
Page Index Toggle Pages: 1