std::list okay to use for Particle System?

Started by
7 comments, last by CoffeeMug 19 years, 6 months ago
I've read that std::vector is bad to use because it was not meant for realtime applications... Just wondering if anybody has had a bad experience using the STL LIST for their particle systems.. I'm about to implement a particle system & I'd like to use STL LIST if there are no known issues. Thanks for the input!
Advertisement
Use std::vector. As long as you remember to reserve() memory and .. (forgot this part)..!

Anyway, STL is faster than they tell you.
It depends on how you traverse the collection of particles each time-step. If you just keep them in the container and update each of them by iterating thru all the elements in the container a list might be more effective. Lists allow you to add and delete elements from anywhere in the list a lot faster than a vector if you have the iterator.

You probably do not want to use a vector where the particles in it has a isDead() method and still traverse them but skip the updating. I'd say a list sounds like a better idea. That way the amount of particles you have to traverse decreases as they die.

You might also run into the situation where the memory free calls will be far slower than just checking if a particle is marked as dead and skipping it. In that case you could keep a vector with the isDead() method.

There's a lot of things you have to consider when choosing your container type. Just profile the cases you can think of and pick the fastest one.

*edit*
Removed comment about using list of pointers as that will still cause a mem free on the list node..
Quote:Original post by asp_
It depends on how you traverse the collection of particles each time-step. If you just keep them in the container and update each of them by iterating thru all the elements in the container a list might be more effective. Lists allow you to add and delete elements from anywhere in the list a lot faster than a vector if you have the iterator.

Of course, with a particle engine, there is no reason to add in the middle or at the beginning.
Quote:
You probably do not want to use a vector where the particles in it has a isDead() method and still traverse them but skip the updating. I'd say a list sounds like a better idea. That way the amount of particles you have to traverse decreases as they die.

You might also run into the situation where the memory free calls will be far slower than just checking if a particle is marked as dead and skipping it. In that case you'd either have to use pointers in a list and delete, or re-use, the memory when there's more time or you could keep a vector with the isDead() method.

When an element dies, swap with the last alive element in the vector. When you encounter your first dead element you're done traversing. Occasionally run a cleanup method that just erases them. It will be faster than using a list and allocating/deallocating for insertion and erasure all the time.
Quote:
There's a lot of things you have to consider when choosing your container type. Just profile the cases you can think of and pick the fastest one.

Yes, this is the best advice of all. Profile first, then optimize.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

I've done some tests and iterating through a vector is ALOT faster than doing so with a list. A vector is almost as fast as a standard array and it is alot more flexible. It would be my choice.
In a thread some time ago we tested vectors vs arrays, and there were equally fast as long as you didn't code it wrong.
One more thing to note. There is a huge, and I mean huge, difference in stl speed when compiled in debug and release modes.

I was profiling my code, and almost decided to get rid of STL since it was seriously killing my CPU performance. Upon compiling in release mode, I found that STL was using almost no CPU, and was in fact ultra fast.

Just something to keep in mind.
Quote:Original post by Ataru
One more thing to note. There is a huge, and I mean huge, difference in stl speed when compiled in debug and release modes.

This is not the case only for STL but for testing in general. Someone once compared testing in debug mode to testing car speed with 2 tons of testing equipment attached to it.
You should never let your fears become the boundaries of your dreams.
Quote:Original post by Anonymous Poster
A vector is almost as fast as a standard array and it is alot more flexible.

Actually a vector is *equally* fast to a standard array when it comes to iterating. It compiles to exactly the same code.

I recommend getting Effective STL by Myers. It will answer a lot of your questions.

This topic is closed to new replies.

Advertisement