c++ - array, std::list, std::vector 插入时间

标签 c++ c++11 data-structures

我正在制作一个测试程序来测量每个容器的存储时间。以下是我的测试代码。

#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++;
}

但是,当我执行代码时,结果似乎与理论有些不同。该列表应该是最快的,但结果表明列表中的插入最慢。你能告诉我为什么吗?

最佳答案

插入 vector 或数组需要移动它后面的所有东西;所以如果在一个随机点,平均需要访问每个元素 1.5 次。 0.5 用于查找位置,0.5*2(读取和写入)用于插入。

插入列表需要每个元素 0.5 次访问(以找到位置)。

这意味着 vector 的元素访问次数只有 3 倍。

列表节点比 vector “节点”(只是元素)大 5 到 9 倍。前向迭代需要读取 3 到 5 倍的内存(元素 16 位和指针 32 到 64 位)。

所以列表解决方案读取/写入更多内存!更糟糕的是,它更稀疏(使用后向指针),并且它可能不会以缓存友好的方式在内存中排列( vector 是连续的;列表节点在线性空间中可能是一团糟)因此困惑具有 cpu 内存缓存预测和负载等。

List 很少比 vector 快;与遍历列表相比,您必须更频繁地插入/删除。

最后,vector 使用指数分配并保留未使用的空间。列表每次分配。调用 new 很慢,而且当您请求更大的 block 时通常不会比请求更小的 block 慢多少。将 vector 一次增加 1 1000 次会导致大约 15 次分配(给予或接受);对于列表,1000 个分配。

关于c++ - array, std::list, std::vector 插入时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43129460/

相关文章:

c++ - 使用 "ifstream"、 "stringstream"和 "rdbuf()"将文件内容读取为字符串时如何检查 I/O 错误?

multithreading - C++11中如何根据特定条件创建一个新线程并稍后加入它?

python - ChainMap.new_child() 返回什么?

c++ - 生成算术运算结果类型的策略?

c++ - 多个线程访问共享资源

java - 删除一个链接如何从链表中删除一个节点?

algorithm - 如何找到数组中给定索引的前一个最小元素?

c++ - 多线程多套接字同时发送/接收

c++ - Getline 没有按预期工作

c++ - 如何在字符串中使用指针?