简体   繁体   English

调整大小并保留在向量中

[英]resize and reserve in vector

Main question 主要问题

Does resize for vector affect performance even if its capacity is bigger than its size? 即使向量的容量大于其大小, resize向量的resize也会影响性能吗?


For example, I want to make a code for pseudo code like following 例如,我想为伪代码编写如下代码

const size_t size_a = 2;
const size_t i_max = 3;
vector<int> a(size_a) = random_vector;
vector<int> b;

for (size_t i=0; i<i_max; i++){
    patch_a_at_the_end_of_b;
}

So, b changes its size every loop but it will monotonically increase and I know the final size of b . 因此, b每次循环都会更改其大小,但是它将单调增加,并且我知道b的最终大小。


Firstly, I make a function concat(a,b) such that patch a to the end of b . 首先,使一个功能concat(a,b)使得补丁a到的端b

void concat(const vector<int> a, vector<int> &b){
    const size_t size_b = b.size();
    b.resize(size_b+a.size());
    for(size_t i=0; i<a.size(); i++)
        b[size_b + i] = a[i];
}

Note that we resize vector every time when b is called by concat . 请注意,每次当concat调用b时,我们都会调整向量的大小。

Then, I add b.reserve in front of for loop in main function. 然后,在主函数的for循环前面添加b.reserve

const size_t size_a = 2;
const size_t i_max = 3;
vector<int> a(size_a) = random_vector;
vector<int> b;
b.reserve(size_a*i_max);

for (size_t i=0; i<i_max; i++){
    concat(a,b);
}

The specification of std::vector s operations probably provide most of the answer to this question. std::vector操作的规范可能为该问题提供了大多数答案。

Firstly, resize() is specified as being void resize(size_type n, value_type val = value_type()) , with requirements; 首先,将resize()指定为void resize(size_type n, value_type val = value_type()) ,并附带要求;

  • If n is less than the current size, the additional elements then the additional elements are removed and destroyed (ie if elements have a destructor, the destructor is invoked). 如果n小于当前大小,则删除其他元素并将其删除(即,如果元素具有析构函数,则调用析构函数)。
  • If n is greater than the current container size, the content is expanded by inserting at the end as many elements as needed to reach the size of n . 如果n大于当前容器的大小,则通过在末尾插入任意数量的元素以达到n的大小来扩展内容。 If val is specified, the new elements are initialized as copies of val , otherwise, they are value-initialized. 如果指定了val ,则将新元素初始化为val副本,否则将对它们进行值初始化。
  • If n is also greater (emphasis mine) than the current container capacity, an automatic reallocation of the allocated storage space takes place. 如果n 也大于当前容器容量(重点是我的),则会自动重新分配已分配的存储空间。

Note that nothing whatsoever is said about reducing the container capacity if its size is reduced. 请注意,如果减小容器的容量,则什么也没有说。

Now, reserve(size_type n) is specified as requesting that the vector capacity be n or more elements, with requirements 现在, reserve(size_type n)被指定为要求向量容量n或更多元素,并有要求

  • If n is greater than the current vector capacity, the function causes the container to reallocate its storage increasing its capacity to n (or greater). 如果n大于当前向量容量,则该函数使容器重新分配其存储,将其容量增加到n(或更大)。
  • In all other cases, the function call does not cause a reallocation and the vector capacity is not affected. 在所有其他情况下,函数调用不会导致重新分配,向量容量也不会受到影响。
  • This function has no effect on the vector size and cannot alter its elements. 此功能对向量size没有影响,并且不能更改其元素。

The second point here may be interpreted as saying that reserve() cannot be used to reduce capacity. 这里的第二点可以解释为说reserve()不能用于减少容量。

Note: I'm paraphrasing in the above, but it is essentially a summary of what the standard requires of resize() and reserve() . 注意:以上是我的解释,但本质上是标准对resize()reserve()要求的摘要。

Putting those points together: The only circumstance in which resize() (or any other operation that increases the size of the vector) is required to increase capacity of the vector is if the new size exceeds the current capacity. 将这些要点放在一起:唯一需要resize() (或任何其他增加向量大小的操作)来增加向量容量的情况是,新大小超过当前容量。

Beyond that, what happens is a quality of implementation concern. 除此之外,所发生的事关实施质量。 However, one logical manner of implementation would be; 但是,一种合理的实施方式是:

  • resize() (and other operations that increase size of the vector) never increases capacity unless the new size exceeds current capacity 除非新的大小超过当前容量,否则resize() (以及其他增加向量大小的操作)将永远不会增加容量
  • reserve() never reduces capacity reserve()永远不会减少容量

In such an implementation, calling reserve() before doing operations that increase size of the vector would give a performance advantage (eliminating need for actual reallocation unless the size ever grows to exceed the capacity specified using reserve() ). 在这样的实现中,在执行增加矢量大小的操作之前调用reserve()会带来性能优势(除非实际大小超过了使用reserve()指定的容量,否则就无需进行实际重新分配)。

Things would potentially be different if an implementation used a different approach, such as reducing capacity of a container at any stage (eg if a call of resize() reduces the size, it also reduces the capacity). 如果实现使用不同的方法,例如在任何阶段减小容器的容量(例如,如果调用resize()减小了尺寸,那么它也减小了容量),情况可能会有所不同。 This might be done in an attempt to minimise total memory usage of the container (ie not keeping additional capacity allocated for as long as the vector itself exists). 这样做可能是为了尽量减少容器的总内存使用量(即,只要vector本身存在,就不保留分配的额外容量)。 This would give a performance hit if the size of the vector oscillates in size (eg increases in some circumstances, decreases in others). 如果向量的大小在大小上振荡(例如,在某些情况下会增加,在其他情况下会减少),这会降低性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 vector::resize() 和 vector::reserve() 之间的选择 - Choice between vector::resize() and vector::reserve() std :: vector调整大小与保留期间的保留时间 - std::vector resize vs reserve during fread std::vector 保留和调整 NUMA 位置的大小 - std::vector reserve & resize NUMA locality 奇怪的内存表现为调整大小并保留关于向量的行为 - strange memory behave of resize and reserve about vector 矢量Resize vs Reserve嵌套向量 - Vector Resize vs Reserve for nested vectors std::vector::resize() 与 std::vector::reserve() - std::vector::resize() vs. std::vector::reserve() 使用 vector::reserve 或 vector::resize 时发生了什么 - What happened when use vector::reserve or vector::resize QVector(或std :: vector)保留(或调整大小)不会释放内存 - QVector(or std::vector) reserve(or resize) down doesn't free memory 如何使用 std::vector <std::tuple<A,B> &gt; 管理内存(调整大小,保留,...),但实际上将 As 保持在 Bs 之前,连续 - How to use std::vector<std::tuple<A,B>> to manage memory (resize, reserve,…), but actually keeping the As before the Bs, contiguously 调整std :: vector大小(通过reserve()或resize())时,默认分配器会做什么? - What does the default allocator do when a std::vector is resized (via either reserve() or resize())?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM