简体   繁体   English

C++ std::unordered_map 比 std::map 慢?

[英]C++ std::unordered_map slower than std::map?

In the example below I was expecting unordered_map to be faster than map , but it's not the case.在下面的示例中,我期望unordered_mapmap快,但事实并非如此。 Why is it so?为什么会这样? Is unordered_map designed for another purpose than being faster than the regular map? unordered_map是否设计用于比常规 map 更快的其他目的?

#include <iostream>
#include <chrono>
#include <unordered_map>
#include <map>


int main()
{
    std::unordered_map<int, double> grid_unmap_int;
    std::map<int, double> grid_map_int;
    
    size_t map_size = 1e7;

    auto t1 = clock();
    for (size_t i = 0; i < map_size; ++i) {
        grid_unmap_int[i] = 1.0;
    }
    auto t2 = clock();


    for (size_t k = 0; k < map_size; ++k) {
        grid_map_int[k] = 1.0;
    }
    auto t3 = clock();

    std::cout << "Insertion \n";
    std::cout << "Filling unordered map int key: " << (t2 - t1) / static_cast<double>(CLOCKS_PER_SEC) << " seconds.\n";
    std::cout << "Filling map int key: " << (t3 - t2) / static_cast<double>(CLOCKS_PER_SEC) << " seconds.\n";
    std::cout << std::endl;

    t1 = clock();
    for (size_t i = 0; i < map_size; ++i) {
        double b = grid_unmap_int[i] ;
    }
    t2 = clock();


    for (size_t k = 0; k < map_size; ++k) {
        double b = grid_map_int[k];
    }
    t3 = clock();

    std::cout << "Retrieve \n";
    std::cout << "Filling unordered map int key: " << (t2 - t1) / static_cast<double>(CLOCKS_PER_SEC) << " seconds.\n";
    std::cout << "Filling map int key: " << (t3 - t2) / static_cast<double>(CLOCKS_PER_SEC) << " seconds.\n";


    return 0;
}

EDIT: the results: enter image description here编辑:结果:在此处输入图像描述

In the example below I was expecting unordered_map to be faster than map , but it's not the case.在下面的示例中,我期望unordered_mapmap快,但事实并非如此。 Why is it so?为什么会这样?

That can be the case, as std::map promises logarithmic lookup time , while std::ordered_map promises constant lookup time on average, worst case linear in the size of the container .情况可能是这样,因为std::map承诺对数查找时间,而std::ordered_map承诺平均查找时间恒定,最坏情况与容器大小成线性关系

Which is a fancy way of saying "It could be better, and it could be worse. Profile it for your specific scenario".这是一种奇特的说法,“它可能会更好,也可能会更糟。针对您的特定场景进行分析”。

Is unordered_map designed for another purpose than being faster than the regular map ? unordered_map是否设计用于比常规map更快的其他目的?

Yes.是的。

std::map can only use keys that are ordered . std::map只能使用有序的密钥。 There must be an established way to sort the type, like the int in your example.必须有一种既定的方法来对类型进行排序,例如您的示例中的int And in return, iterating the std::map gives you that ordering.作为回报,迭代std::map会为您提供该排序。

std::unordered_map can only use keys for which std::hash<Key> is defined. std::unordered_map只能使用定义了std::hash<Key>的键。 The keys do not need to have an established ordering (They could be images, for example. Or colors. Or sounds.) But there must exist some way to convert the values to hashes.键不需要有既定的顺序(例如,它们可以是图像。或 colors。或声音。)但必须存在某种方法将值转换为哈希值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM