vector reserve c++

前端 未结 4 464
春和景丽
春和景丽 2021-01-13 11:13

I have a very large multidimensional vector that changes in size all the time. Is there any point to use the vector.reserve() function when I only know a good approximation

4条回答
  •  抹茶落季
    2021-01-13 12:02

    To help with discussion you can consider the following typedefs:

    typedef std::vector int_t;   // internal vector
    typedef std::vector mid_t; // intermediate
    typedef std::vector ext_t; // external
    

    The cost of growing (vector capacity increase) int_t will only affect the contents of this particular vector and will not affect any other element. The cost of growing mid_t requires copying of all the stored elements in that vector, that is it will require all of the int_t vector, which is quite more costly. The cost of growing ext_t is huge: it will require copying all the elements already stored in the container.

    Now, to increase performance, it would be much more important to get the correct ext_t size (it seems fixed 256*256 in your question). Then get the intermediate mid_t size correct so that expensive reallocations are rare.

    The amount of memory you are talking about is huge, so you might want to consider less standard ways to solve your problem. The first thing that comes to mind is adding and extra level of indirection. If instead of holding the actual vectors you hold smart pointers into the vectors you can reduce the cost of growing the mid_t and ext_t vectors (if ext_t size is fixed, just use a vector of mid_t). Now, this will imply that code that uses your data structure will be more complex (or better add a wrapper that takes care of the indirections). Each int_t vector will be allocated once in memory and will never move in either mid_t or ext_t reallocations. The cost of reallocating mid_t is proportional to the number of allocated int_t vectors, not the actual number of inserted integers.

    using std::tr1::shared_ptr; // or boost::shared_ptr
    typedef std::vector int_t;
    typedef std::vector< shared_ptr > mid_t;
    typedef std::vector< shared_ptr > ext_t;
    

    Another thing that you should take into account is that std::vector::clear() does not free the allocated internal space in the vector, only destroys the contained objects and sets the size to 0. That is, calling clear() will never release memory. The pattern for actually releasing the allocated memory in a vector is:

    typedef std::vector<...> myvector_type;
    myvector_type myvector;
    ...
    myvector.swap( myvector_type() ); // swap with a default constructed vector
    

提交回复
热议问题