为什么分配和某种STD ::对比STD :: vector快?

发布于 2025-01-30 08:29:55 字数 1548 浏览 3 评论 0原文

今天,我只是试图解决编程问题。我注意到向量的分配和分类< vector>比vector< pair< int,pair< int,int,int>。我采用了一些基准测试,并知道嵌套向量代码比给定输入的嵌套对码慢4倍( https:// https:// pastebin.com/izwgnez7 )。

以下是我用于基准测试的代码。

    auto t_start = std::chrono::high_resolution_clock::now();
    vector<pair<int, pair<int, int>>> edges;
    for (int i = 0; i < points.size(); i++)
        for (int j = i + 1; j < points.size(); j++)
            edges.push_back({abs(points[i][0] - points[j][0]) + abs(points[i][1] - points[j][1]), {i, j}});
    sort(edges.begin(), edges.end());
    auto t_end = std::chrono::high_resolution_clock::now();
    double elapsed_time_ms = std::chrono::duration<double, std::milli>(t_end - t_start).count();
    cout << elapsed_time_ms << endl;

    auto t_start1 = std::chrono::high_resolution_clock::now();
    vector<vector<int>> edges1;
    for (int i = 0; i < points.size(); i++)
        for (int j = i + 1; j < points.size(); j++)
            edges1.push_back({abs(points[i][0] - points[j][0]) + abs(points[i][1] - points[j][1]), i, j});
    sort(edges1.begin(), edges1.end());
    auto t_end1 = std::chrono::high_resolution_clock::now();
    double elapsed_time_ms1 = std::chrono::duration<double, std::milli>(t_end1 - t_start1).count();
    cout << elapsed_time_ms1 << endl;

输出:

241.917
1188.11

有人知道为什么性能有很大差异吗?

Today I just tried to solve a problem in programming. I noticed that allocation and sorting of the vector<vector> are much much slower than vector<pair<int, pair<int, int>>. I took some benchmarks and came to know that nested vector code is 4x slower than nested pair code for the given input (https://pastebin.com/izWGNEZ7).

Below is the code I used for benchmarking.

    auto t_start = std::chrono::high_resolution_clock::now();
    vector<pair<int, pair<int, int>>> edges;
    for (int i = 0; i < points.size(); i++)
        for (int j = i + 1; j < points.size(); j++)
            edges.push_back({abs(points[i][0] - points[j][0]) + abs(points[i][1] - points[j][1]), {i, j}});
    sort(edges.begin(), edges.end());
    auto t_end = std::chrono::high_resolution_clock::now();
    double elapsed_time_ms = std::chrono::duration<double, std::milli>(t_end - t_start).count();
    cout << elapsed_time_ms << endl;

    auto t_start1 = std::chrono::high_resolution_clock::now();
    vector<vector<int>> edges1;
    for (int i = 0; i < points.size(); i++)
        for (int j = i + 1; j < points.size(); j++)
            edges1.push_back({abs(points[i][0] - points[j][0]) + abs(points[i][1] - points[j][1]), i, j});
    sort(edges1.begin(), edges1.end());
    auto t_end1 = std::chrono::high_resolution_clock::now();
    double elapsed_time_ms1 = std::chrono::duration<double, std::milli>(t_end1 - t_start1).count();
    cout << elapsed_time_ms1 << endl;

Output:

241.917
1188.11

Does anyone know why there is a big difference in performance?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

送舟行 2025-02-06 08:29:55

std :: Pairstd :: Array在编译时已知固定大小,并且将直接在类本身中包含对象。另一方面,std :: vector必须处理动态大小,并且需要在堆上分配大量内存以容纳对象。

对于小对象,std :: Pairstd :: array会更好,因为分配和释放内存的开销会陷入您的性能中。那就是您所看到的。当比较元素以及必须在运行时检查大小时,指针涉及的额外间接方向也将使您付出代价。

另一方面,对于大对象,std :: vector应该更好,因为它支持移动语义。交换2个向量只会将指针交换为数据,而std :: Pairstd:array将必须移动/复制每个元素,这对于大对象来说是昂贵的。

因此,您所看到的不是该对比向量快,而是该对中的速度比向量更快。

A std::pair or std::array has a fixed size known at compile time and will include the objects directly in the class itself. A std::vector on the other hand has to deal with dynamic size and needs to allocate a chunk of memory on the heap to hold the objects.

For small objects the std::pair or std::array will be better because the overhead of allocating and freeing memory will eat into your performance. That's what you are seeing. The extra indirection involved with the pointer will also cost you when e.g. comparing the elements as well as having to check the size at run time.

On the other hand for large objects the std::vector should be better because it supports move semantics. Swapping 2 vectors will just swap the pointer to the data while std::pair or std:array will have to move/copy each element, which would be costly for large objects.

So what you see is not that pair is faster than vector but that pair is faster than vector in that use case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文