vendredi 25 novembre 2022

which code is better when we use c++ std::unordered_map?

Here is the code.

1.

std::unordered_map<std::string, std::unordered_map<std::string, double>> ab_map;
for (int i = 0; i < 30000; i++) {
    std::unordered_map<std::string, double>& item = ab_map[std::to_string(i)];
    for (int j = 0; j < 10; j++) {
        item.emplace(std::to_string(j), 10.0);
    }
}
    std::unordered_map<std::string, std::unordered_map<std::string, double>> ab_map;
    for (int i = 0; i < 30000; i++) {
        std::unordered_map<std::string, double> item;
        for (int j = 0; j < 10; j++) {
            item.emplace(std::to_string(j), 10.0);
        }
        ab_map.emplace(std::to_string(i), std::move(item));
    }
    

    I think the second usage is better. But after executing the code and calculating the cost time. I found that the first one is better. Why?

    test1: first: 183947 microseconds; second: 186965 microseconds

    test2: first: 226307 microseconds; second: 268437 microseconds

    Aucun commentaire:

    Enregistrer un commentaire