簡體   English   中英

數組,std :: list,std :: vector插入時間

[英]array, std::list, std::vector inserting time

我正在制作一個測試程序,以測量每個容器的存儲時間。 以下是我的測試代碼。

#include <list>
#include <vector>
#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;

void insert(list<short>& l, const short& value);
void insert(vector<short>& v, const short& value);
void insert(short arr[], int& logicalSize, const int& physicalSize, const short& value);

int main() {
    clock_t start, end;
    srand(time(nullptr));

    const int SIZE = 50000;
    const short RANGE = 10000;
    list<short> l;
    vector<short> v;
    short* arr = new short[SIZE];
    int logicalSize = 0;

    // array
    start = clock();
    cout << "Array storage time test...";
    for (int i = 0; i < SIZE; i++) {
        try {
            insert(arr, logicalSize, SIZE, (short)(rand() % (2 * RANGE + 1) - RANGE));
        } catch (string s) {
            cout << s << endl;
            system("pause");
            exit(-1);
        }
    }
    end = clock();
    cout << "Time: " << difftime(end, start) << endl << endl;

    // list
    cout << "List storage time test...";
    start = clock();
    for (int i = 0; i < SIZE; i++) {
        insert(l, (short)(rand() % (2 * RANGE + 1) - RANGE));
    }
    end = clock();
    cout << "Time: " << difftime(end, start) << endl << endl;

    // vector
    cout << "Vector storage time test...";
    start = clock();
    for (int i = 0; i < SIZE; i++) {
        insert(v, (short)(rand() % (2 * RANGE + 1) - RANGE));
    }
    end = clock();
    cout << "Time: " << difftime(end, start) << endl << endl;



    delete[] arr;
    system("pause");
    return 0;
}

void insert(list<short>& l, const short& value) {
    for (auto it = l.begin(); it != l.end(); it++) {
        if (value < *it) {
            l.insert(it, value);
            return;
        }
    }
    l.push_back(value);
}

void insert(vector<short>& v, const short& value) {
    for (auto it = v.begin(); it != v.end(); it++) {
        if (value < *it) {
            v.insert(it, value);
            return;
        }
    }
    v.push_back(value);
}

void insert(short arr[], int& logicalSize, const int& physicalSize, const short& value) {
    if (logicalSize == physicalSize) throw string("No spaces in array.");
    for (int i = 0; i < logicalSize; i++) {
        if (value < arr[i]) {
            for (int j = logicalSize - 1; j >= i; j--) {
                arr[j + 1] = arr[j];
            }
            arr[i] = value;
            logicalSize++;
            return;
        }
    }
    arr[logicalSize] = value;
    logicalSize++;
}

但是,當我執行代碼時,結果似乎與理論有所不同。 該列表應該最快,但是結果表明插入列表的速度最慢。 你能告訴我為什么嗎?

插入向量或數組時需要移動所有內容; 因此,如果在隨機位置上,平均每個元素需要訪問1.5次。 0.5查找點,0.5 * 2(讀寫)進行插入。

插入列表需要每個元素0.5次訪問(以查找位置)。

這意味着向量僅是元素訪問的3倍。

列表節點比矢量“節點”(只是元素)大5到9倍。 正向迭代需要讀取3到5倍的內存(元素16位和指針32位到64位)。

因此,列表解決方案讀取/寫入更多的內存! 更糟糕的是,它比較稀疏(帶有后向指針), 並且可能不會以緩存友好的方式在內存中排列(向量是連續的;列表節點在線性空間中可能是一團糟),從而使cpu內存緩存預測和負載陷入混亂等等。

List很少比vector快。 與在列表上進行迭代相比,您插入/刪除的次數要多得多。

最后,向量使用具有未使用的保留空間的指數分配。 列表每次分配。 調用new的速度很慢,當您請求更大的塊時,調用它的速度通常不會比您請求較小的塊時慢很多。 將向量一次增加1乘以1000次會產生大約15個分配(給定或獲取); 對於列表,有1000個分配。

插入list的速度非常快,但是首先必須找到要插入的列表。 這是list失敗者的出處。

停止閱讀以下內容可能會有所幫助: 為什么處理排序數組要比未排序數組快? 現在大概是某個時候,因為它涵蓋了相似的材料並且覆蓋得非常好。

通過vector或數組,每個元素都在下一個之后。 預測非常簡單,因此CPU可以在處理當前值的同時,將一段時間不需要的值加載到緩存中。

有了list可預測性,您必須先獲得下一個節點,然后才能加載該節點,這幾乎使緩存無效。 如果沒有高速緩存,您會發現性能下降了一個數量級,因為CPU 處於等待狀態,等待從RAM中檢索數據。

Bjarne Stroustrup在這個話題上有很多更長的篇幅 主題視頻絕對值得一看。

一個重要的收獲是采用Big-O表示法,因為它衡量的是算法的效率,而不是算法利用硬件的程度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM