STL vector (4) Why should vector expansion be expanded by 1.5 times or 2 times?

      We know that the vector will expand when needed, 1.5 times under VS and 2 times under GCC. Then two problems arise:

(1) Why is it growing exponentially instead of growing a fixed-size capacity each time?

(2) Why is it a 2-fold or 1.5-fold increase instead of a 3-fold or 4-fold increase?

1. The first question: 

      If it has grown exponentially. Assuming that there are n elements, the multiplication factor is m; to complete the push_back​ operation of these n elements into a vector, the number of times that memory needs to be reallocated is about logm(n); the i-th reallocation will result in copying m^( i) (that is, the current vector.size() size) elements in the old space; the time taken by n push_back operations is O(n), as shown in the following figure:


                  

m / (m - 1), this is a constant, the method of amortization analysis shows that the time complexity of the push_back operation in the vector is constant time.
      ​If the fixed value is increased at one time. Assuming that there are n elements, each time increases by k; the number of copies for the i-th increase is: 100i; the time complexity of n push_back operations is O(n^2):
                       
the time for each push_back operation is evenly amortized The complexity is O(n);
summary: By comparison, it can be found that the expansion of the capacity by doubling can guarantee a constant time complexity, while increasing the capacity of the specified size can only reach the time complexity of O(n). times the expansion.


2. The second question

      Someone replied: The most important thing about vector lies in the query, use the shift (power of 2) to directly get the hash chain and the length of the node, and then subtract it directly to get the key value, the complexity is O(2), the performance is similar to the array, insertion and deletion Can be dynamic, which is the basic purpose of vector design.

      Obviously, the multiple of growth cannot be very large, and it cannot be smaller than 1, so what is the optimal upper limit for it? If the capacity is expanded by more than 2 times, the memory requested for the next time will be larger than the sum of the previously allocated memory, resulting in that the previously allocated memory can no longer be used. So, the best growth factor is between (1, 2).

      

There is an explanation on Zhihu:

https://www.zhihu.com/question/36538542/answer/67929747


     When k = 1.5, after several expansions, the previous memory space can be reused

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324525736&siteId=291194637