wood burning stoves 2.0*
The moose likes Performance and the fly likes Particle System Optimisation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Particle System Optimisation" Watch "Particle System Optimisation" New topic
Author

Particle System Optimisation

Joshua Waring
Greenhorn

Joined: Sep 04, 2012
Posts: 13
I've created a particle system in java (OBVIOUSLY -facepalm) and the code should be explanatory since I put a lot of comments inside, but I was wondering if the opproach was rather efficient in memory use and calculations ect


Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3003
    
    9
Hmmm, there are a number of strange things going on here. It's confusing to see local variables named in ALL_CAPS, because standard Java naming convention is to use all caps only for constants. And array indices start at 0 and end with array.length - 1, so the normal way to loop is

or using even more modern Java:

And note that I'm assuming a class Particle to represent the properties of a particle. This array-of-arrays stuff is very strange, when each element in the array represents something different. Use object-oriented programming and give your objects fields with useful names, not mysterious magic numbers.

As for your main question, it's true that array resizing can be tediously slow for a large array as you have here. Your solution is interesting, but believe it's needlessly complex. I think a good simple alternative would be to use a LinkedList to contain all the particles. When you need to remove one, be sure to call remove through the LinkedList's Iterator rather than using LinkedList.remove(), and you will find it's very quick for removing elements while iterating through. And new points can simply be added at the end. Yes, you'll be creating a new Particle object each time, and letting one be garbage collected each time one is removed - but this really isn't a big deal in Java. Avoiding the big delay of array resizing is good. Avoiding small delays of object creation is usually a bad idea. You will spend much more time debugging your code than you would if you make use of existing libraries like LinkedList.
Joshua Waring
Greenhorn

Joined: Sep 04, 2012
Posts: 13
Guess that's what I get for being a beginner, I've never heard of a LinkedList but after a quick look the principle is easily understood.
Joshua Waring
Greenhorn

Joined: Sep 04, 2012
Posts: 13
Okay So I've created a very basic version of the above (no gravity or cull, no cull since I don't test the performance difference Yet at a point where they would be cullable)
so I've gone from about 4ms renders to 80ms, and increasing with more particles.
I think making too many objects has become a problem, since my other method was fast, as in really fast.
I thought it would be nice, because this IS my first time using another class and object in such way.

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

Joshua Waring wrote:I've created a particle system in java...

Joshua.

Please re-read the UseCodeTags page thoroughly.

I've broken up those enormous comment lines of your this time because it makes the thread very difficult to read, but I won't do it again.

Thanks.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

Joshua Waring wrote:Okay So I've created a very basic version of the above (no gravity or cull, no cull since I don't test the performance difference Yet at a point where they would be cullable)
so I've gone from about 4ms renders to 80ms, and increasing with more particles.
I think making too many objects has become a problem, since my other method was fast, as in really fast.
I thought it would be nice, because this IS my first time using another class and object in such way.

First: STOP CODING.

Second: Re-read Mike's post carefully.

Third: Write down what you're trying to do in English. And be specific.
A few things I can think of to describe:
1. What is this array supposed to represent? Presumably a collection of particles; but what else?
2. What is all this active/inactive business? Again, presumably a particle starts out life as 'active', but what causes it to become inactive?
3. What is this culling? Is it simply the removal of inactive particles, or is there something more to it?

Fourth: Class names should start with a CAPITAL letter. I also think you've hamstrung yourself by naming your application class 'particle'; I think a much better name would be something like ParticleMovementSystem, or something else that describes what it's doing (see point 1 above). Then, you can create a class that defines your individual Particles, and call it Particle.

Fifth: The position of a particle appears to be quite important, so you might want to think about creating a class that represents that too (Position?).

Sixth: DON'T OBSESS ABOUT SPEED. Right now, you're a long way from a proper design; and the ONLY time you should be thinking about speed is when you've got the design correct and have a working prototype.

Seventh: If one of your rules is that two particles cannot occupy the same space, then another possible structure to look at might be a HashMap; and if the array represents a cache (which it looks like it might), you could use a LinkedHashMap.

Eighth: If I'm right about 'seventh', what does a particle 'collision' (two particles ending up in the same Position) result in? How do you want to handle it?

And that's just for starters.

Programming is about thinking, NOT coding.

Winston
Joshua Waring
Greenhorn

Joined: Sep 04, 2012
Posts: 13
I've changed my direction of programming, guess having a working system is better than having a fast system without function.
So for particle collision, I guess the way to go would be split the area into sections and test for collision of particles in those positions instead of all with all.
I've rearranged some names and joined Linked List's with my arrays, which is faster than a object.

Though using the iteration it's giving me an error, which doesn't make sense, since the condition is interation.hasNext but gives an error on the .next(), not right away but after a few hundred cycles.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

Joshua Waring wrote:I've changed my direction of programming, guess having a working system is better than having a fast system without function.
...
I've rearranged some names and joined Linked List's with my arrays, which is faster than a object.

I doubt that you can say that categorically; and the result of that thinking is that you're still obsessing about implementation rather than design.

My 7-year old Dell can create 20 million objects a second, so 50,000, even with a bit of constructor code, is probably going to run in less than 1/100th of a second.

Once again: Your array-based solution is flawed and brittle. Stop worrying about CPU cycles and get the design right. And to do that, you have to stop codiing and think.

W.A. Wulf wrote:More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason — including blind stupidity.


Winston
 
Consider Paul's rocket mass heater.
 
subject: Particle System Optimisation
 
Similar Threads
cant get counters to work correctly
util.Random problems (?)
Why synchronization here?
multithreading not working correctly
Particle System, always equals 0 ?