Should I use an array for a vector for performance?

Possible duplicate:
std :: vector is much slower than simple arrays?

It looks like the vector is allocated on the heap instead of the stack.

So, should you use an array to replace the vector (if possible) when performance becomes a serious problem?

+5
source share
3 answers

No. (to satisfy the pedants' remarks, no, you should not “prefer” arrays over vectors for performance, but, of course, you should “consider” using arrays to replace vectors for specific cases described below)

, , , .

( ) , .

, , :

  • , , .
  • , .

, , .

, , , , ( std::array) .

? ...

+19

, , , . ++ 11 :

std::array<int,10000> arr;

:

int arr[10000]; //avoid it in C++11 (strictly), and possibly in C++03 also!

++ 03 std::vector ( , ):

std::vector<int> arr;
arr.reserve(10000); //it is good if you know the (min) number of items!

, , , reserve(). --, .

+5

If you work in an exceptional system (i.e., with a slow memory allocation), this is unlikely to be significant. I suggest you use std :: vector instead to provide a more secure type of security than simple arrays.

0
source

All Articles