vector<> VS <> deque<> VS list<>

来源:互联网 发布:丁香诊所怎么样 知乎 编辑:程序博客网 时间:2024/05/10 12:28

Here is the summary from an interesting performance benchmark article which comparing vector and list int C++11

Conclusion for vector<> VS deque<> VS list<>:

To conclude, we can get some facts about each data structure:

  • std::list is very very slow to iterate through the collection due to its very poor spatial locality.
  • std::vector and std::deque perform always faster than std::list with very small data
  • std::list handles very well large elements
  • std::deque performs better than a std::vector for inserting at random positions (especially at the front, which is constant time)
  • std::deque and std::vector do not support very well data types with high cost of copy/assignment

This draw simple conclusions on usage of each data structure:

  • Number crunching: use std::vector or std::deque
  • Linear search: use std::vector or std::deque
  • Random Insert/Remove: use std::list (if data size very small (< 64B on my computer), use std::deque)
  • Big data size: use std::list (not if intended for searching)
  • Non-trivial data type: use std::list
  • Push to front: use std::deque or std::list
Updated conclusion for vector<> VS list<>:
  • std::vector is insanely faster than std::list to find an element
  • std::vector performs always faster than std::list with very small data
  • std::vector is always faster to push elements at the back than std::list
  • std::list handles very well large elements, especially for sorting or inserting in the front

This draw simple conclusions on usage of each data structure:

  • Number crunching: use std::vector
  • Linear search: use std::vector
  • Random Insert/Remove: use std::list (if data size very small (< 64B on my computer), use std::vector)
  • Big data size: use std::list (not if intended for searching)

Reference:
http://www.baptiste-wicht.com/2012/11/cpp-benchmark-vector-vs-list/
http://www.baptiste-wicht.com/2012/12/cpp-benchmark-vector-list-deque/