Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Standard vector and boost array: which is faster?

How the performance of boost::array compares to that of std::vector, and which factors have significant influence on it?

like image 548
grzkv Avatar asked Feb 14 '11 16:02

grzkv


People also ask

Which is faster array or vector?

A std::vector can never be faster than an array, as it has (a pointer to the first element of) an array as one of its data members. But the difference in run-time speed is slim and absent in any non-trivial program. One reason for this myth to persist, are examples that compare raw arrays with mis-used std::vectors.

Is it better to use array or vector?

Vector is better for frequent insertion and deletion, whereas Arrays are much better suited for frequent access of elements scenario. Vector occupies much more memory in exchange for managing storage and growing dynamically, whereas Arrays are a memory-efficient data structure.

How much slower are vectors than arrays?

That's about 3 - 4 times slower!

Does vector take more memory than array?

Vectors are efficient and flexible. They do require a little more memory than arrays, but this tradeoff is almost always worth the benefits.


2 Answers

boost::array (or C++0x's std::array) should be faster than std::vector because boost::array instances are entirely on the stack. This means boost::array has no heap allocation, and it also means it can't grow past the size you specify for it at construction.

The purpose of boost::array is to serve as a thin layer around primitive arrays, so you can treat them as standard containers with .begin(), .end() etc. Good compilers should eliminate all overhead of boost::array such that it performs identically to primitive arrays.


All this concerning "default" setup, where you don't have custom allocators and you measure simple things like array construction, access and modification of elements. On the other hand, things can turn around in other tests, other platforms or with a clever setup. For example,

  • if you create a custom allocator, perhaps acquiring a large memory pool at program startup, then constructing or resizing a std::vector might not any more be all that expensive.
  • Swapping one std::vector with another is normally a very fast operation; the speed of swapping two pointers. Swapping two boost::array instances might be much more expensive; in the order of copying n elements. But then, in C++0x, of which std::array will be a part, swapping two arrays will be fast again, thanks to rvalue references and their move semantics.
  • Copying a vector might be a very fast operation; as fast as copying a pointer (copy on write). Copying a boost::array might require copying each array element. Then again, sometimes copying any object is very fast, even faster than copying a pointer and even in your C++03 compiler -- thanks to copy elision.

You can profile to see which is faster for your use, but even this test will only give you an idea for a particular version of a particular compiler on a particular platform.

like image 135
wilhelmtell Avatar answered Oct 13 '22 06:10

wilhelmtell


The best way to reach any conclusion is writing programs to test their performance with huge amount of data. How else one can arrive at any conclusion?

While you're at it, you may need some tools to assist you, such as VTune, or AMD CodeAnalyst Performance Analyzer, etc. Very Sleepy (free tool) is a C/C++ CPU profiler for Windows systems. You may try them!

like image 25
Nawaz Avatar answered Oct 13 '22 06:10

Nawaz