Jump to content
  • Advertisement
Sign in to follow this  
oxygen728

std::list okay to use for Particle System?

This topic is 5403 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Advertisement
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..

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!