【翻译】不要在STL vector上浪费时间

 本文翻译自:https://lemire.me/blog/2012/06/20/do-not-waste-time-with-stl-vectors/

作者主要说了vector的两个弊端,一个就是它的容量永远不会减少,即使调用了clear函数(这点已经在C++11中通过shrink_to_fit方法解决);

原文:When using the vector template in high performance code, we must be aware of several important facts. For example, the capacity (memory usage) of a vector will never decrease, even if you call the clear method, unless you use the swap method. In my opinion, making it difficult to release memory was a poor design decision. (This was fixed in the C++11 language update with the addition of the shrink_to_fit method.) Another problem with the vector template is that whenever you copy a vector, you copy all the data it contains. Again, to avoid this problem, you must use the swap method.

第二个就是它的效率不高。然后作者通过计算1-N的数列的和:

使用C++的new:

1 int * bigarray = new int[N];
2 for(unsigned int k = 0; k<N; ++k)
3   bigarray[k] = k;
4 int sum = total(bigarray,N);
5 delete [] bigarray;
6 return sum;

使用等效的STL vector:

vector<int> bigarray(N);
for(unsigned int k = 0; k<N; ++k)
  bigarray[k] = k;
int sum = total(bigarray,N);
return sum;

使用纯粹的STL代码:

1 vector<int> bigarray;
2 for(unsigned int k = 0; k<N; ++k)
3   bigarray.push_back(k);
4 int sum = total(bigarray,N);
5 return sum;

在已知vector容量的情况下进行优化:

1 vector<int> bigarray;
2 bigarray.reserve(N);
3 for(unsigned int k = 0; k<N; ++k)
4   bigarray.push_back(k);
5 int sum = total(bigarray,N);
6 return sum;

然后作者统计了在它的桌面i7 CPU上每一次循环的CPU周期次数:

method cycles per integer 
C++ new 8.5
STL vector 8.9
push_back 20.6
reserve + push_back 12.6

可以看到,使用C++ new,即使用数组的方式是最快的。其次是把vector当做数组一样的使用方法;当vector容量未知,需要不断扩容的时候,使用push_back是最慢的;而当能够确定vector的容量时,使用push_back的效率又会好一些。

 

 

 

posted @ 2018-04-28 09:28  willhua  阅读(575)  评论(0)    收藏  举报