[英]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())
,并附带要求;
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
小于当前大小,则删除其他元素并将其删除(即,如果元素具有析构函数,则调用析构函数)。 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
副本,否则将对它们进行值初始化。 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
或更多元素,并有要求
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.