vector being faster to build or clear than deque or list is to be expected; it's a simpler data structure.
With regard to vector::push_back, it has to do two things:
check the vector is big enough to hold the new item.
insert the new item.
You can generally speed things up by eliminating step 1 by simply resizing the vector and using operator[] to set items.
UPDATE: Original poster asked for an example. The code below times 128 mega insertions, and outputs
push_back : 2.04s
reserve & push_back : 1.73s
resize & place : 0.48s
when compiled and run with g++ -O3 on Debian/Lenny on an old P4 machine.
With regard to vector::push_back, it has to do two things:
check the vector is big enough to hold the new item.
insert the new item.
You can generally speed things up by eliminating step 1 by simply resizing the vector and using operator[] to set items.
UPDATE: Original poster asked for an example. The code below times 128 mega insertions, and outputs
push_back : 2.04s
reserve & push_back : 1.73s
resize & place : 0.48s
when compiled and run with g++ -O3 on Debian/Lenny on an old P4 machine.
#include <iostream> #include <time.h> #include <vector> int main(int,char**) { const size_t n=(128<<20); const clock_t t0=clock(); { std::vector<unsigned char> a; for (size_t i=0;i<n;i++) a.push_back(i); } const clock_t t1=clock(); { std::vector<unsigned char> a; a.reserve(n); for (size_t i=0;i<n;i++) a.push_back(i); } const clock_t t2=clock(); { std::vector<unsigned char> a; a.resize(n); for (size_t i=0;i<n;i++) a[i]=i; } const clock_t t3=clock(); std::cout << "push_back : " << (t1-t0)/static_cast<float>(CLOCKS_PER_SEC) << "s" << std::endl; std::cout << "reserve & push_back : " << (t2-t1)/static_cast<float>(CLOCKS_PER_SEC) << "s" << std::endl; std::cout << "resize & place : " << (t3-t2)/static_cast<float>(CLOCKS_PER_SEC) << "s" << std::endl; return 0; }
http://www.cnblogs.com/biyeymyhjob/archive/2012/09/12/2674004.html
1.vector的內存增長
vector其中一個特點:內存空間只會增長,不會減小,援引C++ Primer:為了支持快速的隨機訪問,vector容器的元素以連續方式存放,每一個元素都緊挨着前一個元素存儲。設想一下,當vector添加一個元素時,為了滿足連續存放這個特性,都需要重新分配空間、拷貝元素、撤銷舊空間,這樣性能難以接受。因此STL實現者在對vector進行內存分配時,其實際分配的容量要比當前所需的空間多一些。就是說,vector容器預留了一些額外的存儲區,用於存放新添加的元素,這樣就不必為每個新元素重新分配整個容器的內存空間。
在調用push_back時,每次執行push_back操作,相當於底層的數組實現要重新分配大小;這種實現體現到vector實現就是每當push_back一個元素,都要重新分配一個大一個元素的存儲,然后將原來的元素拷貝到新的存儲,之后在拷貝push_back的元素,最后要析構原有的vector並釋放原有的內存。例如下面程序:
復制代碼
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;
class Point
{
public:
Point()
{
cout << "construction" << endl;
}
Point(const Point& p)
{
cout << "copy construction" << endl;
}
~Point()
{
cout << "destruction" << endl;
}
};
int main()
{
vector<Point> pointVec;
Point a;
Point b;
pointVec.push_back(a);
pointVec.push_back(b);
cout<<pointVec.size()<<std::endl;
return 0;
}
復制代碼
輸出結果:
其中執行
pointVec.push_back(a);
此時vector會申請一個內存空間,並調用拷貝構造函數將a放到vector中
再調用
pointVec.push_back(b);
此時內存不夠 需要擴大內存,重新分配內存 這時再調用拷貝構造函數將a拷貝到新的內存,再將b拷入新的內存,同時有人調用Point拷貝構造函數,最后釋放原來的內存 此時調用Point的析構函數。
2.vector的內存釋放
由於vector的內存占用空間只增不減,比如你首先分配了10,000個字節,然后erase掉后面9,999個,留下一個有效元素,但是內存占用仍為10,000個。所有內存空間是在vector析構時候才能被系統回收。empty()用來檢測容器是否為空的,clear()可以清空所有元素。但是即使clear(),vector所占用的內存空間依然如故,無法保證內存的回收。
如果需要空間動態縮小,可以考慮使用deque。如果vector,可以用swap()來幫助你釋放內存。具體方法如下:
vector<Point>().swap(pointVec); //或者pointVec.swap(vector<Point> ())
標准模板:
復制代碼
template < class T >
void ClearVector( vector< T >& vt )
{
vector< T > vtTemp;
veTemp.swap( vt );
}
復制代碼
swap()是交換函數,使vector離開其自身的作用域,從而強制釋放vector所占的內存空間,總而言之,釋放vector內存最簡單的方法是vector<Point>().swap(pointVec)。當時如果pointVec是一個類的成員,不能把vector<Point>().swap(pointVec)寫進類的析構函數中,否則會導致double free or corruption (fasttop)的錯誤,原因可能是重復釋放內存。(前面的pointVec.swap(vector<Point> ())用G++編譯沒有通過)
3.其他情況釋放內存
如果vector中存放的是指針,那么當vector銷毀時,這些指針指向的對象不會被銷毀,那么內存就不會被釋放。如下面這種情況,vector中的元素時由new操作動態申請出來的對象指針:
#include <vector>
using namespace std;
vector<void *> v;
每次new之后調用v.push_back()該指針,在程序退出或者根據需要,用以下代碼進行內存的釋放:
復制代碼
for (vector<void *>::iterator it = v.begin(); it != v.end(); it ++)
if (NULL != *it)
{
delete *it;
*it = NULL;
}
v.clear();