Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Bleakcabal

Which is faster stl:list or stl:vector

This topic is 5365 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 want to use the fastest one of the two to store my GameObject classes. I don''t think I really need to be using one or to other.

Share this post


Link to post
Share on other sites
Advertisement
If you need to insert/delete objects in the middle of the container, use a list. Otherwise, use a vector.

Share this post


Link to post
Share on other sites
Which is faster depends on how you use them. std::vectors tend to give better performance accessing and iterating and inserting/deleting at the end. std::lists tend to give better performance for insertion and deletion at the front and middle. Some algorithms you can only use on a vector and not on a list.

Share this post


Link to post
Share on other sites
Short answer:Both.

Long answer: How are you using the container, are there frequent inserts, many inserts in the middle, is random access required, etc. etc. Decide how you are using the container before choosing a particular one. Each has it''s own strengths and weaknesses.

Share this post


Link to post
Share on other sites
quote:
Original post by antareus
To say nothing of a deque! The oft-ignored dunce of the containers!


And there you go, spilling out all our secrets.

Share this post


Link to post
Share on other sites
I don''t care about inserting at the middle but I will need to delete anywhere so I guess I''m better off with a list.

Share this post


Link to post
Share on other sites
That depends on how often you need to delete compared to how often you need to iterate / access and also on how big the objects you''re storing are. In a typical case you''ll be storing GameObject pointers (because GameObject is a base class with virtual functions) and you''ll be iterating more frequently than you''ll be deleting. Since pointers are small (and therefore cheap to copy when you delete from the middle) a vector is probably a better choice than a list in that case. A list also wastes space when storing small objects - for a list of pointers you have two list pointers for every pointer you''re storing...

Share this post


Link to post
Share on other sites
Depending on if the order of objects in the container is important or not, you can get reasonably fast deletion in the middle of a vector by swapping the element to be deleted with the last element of the vector and then calling pop_back().

Share this post


Link to post
Share on other sites
Just use a std::vector first, then, if the program becomes too slow, you could try a std::list to see if it''s faster. If you put a typedef std::vector GameObjects; in a header file all you need to do to change to list is that line. The difference between the two containers is probably not significant, don''t waste your time on it.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!