C++標準庫 std::list 與 std::vector效能對比__C++

來源:互聯網
上載者:User

list與vector分別通過鏈表和數組實現,所以list進行刪除、插入操作時效率要比vector高出許多,而vector進行隨機訪問時要比list高,可是當進行順序添加和順序遍曆時的效率兩者的效率又是誰高呢。

首先分析一下,

1)對於順序追加的操作,當vector預先分配的記憶體不夠時,需要重新分配記憶體並複製對象,會對效率產生負面的影響;而list在每添加一個對象時都必須動態分配,每次動態分配記憶體都需要消耗系統CPU時間,這也是嚴重影響list效率的問題,所以list的運行效率反而可能比vector的還要低。而從另外一角度,list每個對象都必須有指向下一個對象的指標,所以每個對象都要比vector多佔用至少一個指標大小的記憶體。

2)對於順序遍曆操作,兩都應該差別不大,內為都是進行簡單的指標運算。下面通過程式進行測試。

/**
* list_vs_vector.cpp
* created:    2009-10-26 11:57
* author: Noock Tian (noock.tian@gmail.com)
* A performance test program for std::list vs std::vector
*/
#include
#include
#include
#include
#include
#include
using namespace std;

const char* test_data[]={
    "Hello"
    , "Hello C++ guys"
    , "Hello C++ guys from Noock Tian"
    , "Hello C++ from Noock Tian in Zheng Zhou , Henan Province, China"
    , "This is a test string as a string object for performance testing between std::vector and std::list"
};

template
void test_add( long long repeat)
{
    cout<<"Start test_add("<    size_t tstart = clock(), tend;
    {
        T v;
        for( long long ll = 0; ll            for( int i=0; i                v.push_back(string(test_data[i]));
            }
        }
    }

    tend = clock();
    cout<<"Result: start = "<    T v;
    for( int i=0; i        v.push_back(string(test_data[i]));
    }
    size_t tstart = clock(), tend;
    for( long long ll = 0; ll        for(typename T::const_iterator it = v.begin(); it != v.end(); ++it){
            strcpy((char*)null_buff, it->c_str());
        }
    }
    tend = clock();
    cout<<"Result: start = "<    test_add >(repeat);
    test_sequantial_traval >(repeat);

    cout<<"/n========= list =========="<    test_add >(repeat);
    test_sequantial_traval >(repeat);

    int c;
    cout<<"enter a letter to exit"<    cin>>c;

    return 0;
}

在VS2010beta2上的結果如下:

=========vector:==========
Start test_add(100000)
Result: start = 27; end = 401; cost = 374
Start test_travel(100000)
Result: start = 438; end = 473; cost = 35 ========= list ==========
Start test_add(100000)
Result: start = 494; end = 881; cost = 387
Start test_travel(100000)
Result: start = 902; end = 940; cost = 38
在cygwin中,使用GCC 3.4.4測試結果如下:
=========vector:==========
Start test_add(100000)
Result: start = 31; end = 2916; cost = 2885
Start test_travel(100000)
Result: start = 2916; end = 2978; cost = 62 ========= list ==========
Start test_add(100000)
Result: start = 2978; end = 5521; cost = 2543
Start test_travel(100000)
Result: start = 5521; end = 5584; cost = 63
結果顯示,在只進行順序添加和順序遍曆的情況下兩者運行效率相當,其效率與編譯器與其標準庫的實現有關係,但list將佔用更多的記憶體,而且可能造成更多的記憶體片段,所以使用vector是更好的選擇。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.