vector的效率










今天改了一个程序,声明一个vector<vector<int> > d_matrix;



vector<int> a(100);
int b[100];
 
struct timeval ss, se, us, ue;
gettimeofday(&ss, NULL);
vector<int>::iterator it;
for (int i=0; i<1000000; i++) {
   for (int j=0;j<100; j++) {//it=a.begin(); it<a.end(); it++) {
       a[j] = 1;
    }
 }
gettimeofday(&se, NULL);

gettimeofday(&us, NULL);
for (int i=0; i<1000000; i++) {
     for (int j=0; j<100; j++) {
         b[j] = 1;
      }
 }
 
gettimeofday(&ue, NULL);


vector operation: 6067.690000 ms, 606.769000 us per req
array operation: 468.589000 ms, 46.858900 us per req

相差10几倍。在一个内部循环中(如果超过亿数量级),用vector和数组差别还是很大的。
对于一个100个元素的数组,做循环,vector大概要用到10m/亿次, 数组用到45s/亿次 左右。

猜你喜欢

转载自blog.csdn.net/cxf7394373/article/details/20642245