c++2022. 11. 2. 21:30

#include <vector>
#include <iostream>
#include <map>
#include <set>
#include <unordered_map>
#include <random>
#include <chrono>

static const long long mapSize = 10000000;
static const long long accSize = 1000000;

int main()
{
    std::map<int, int> myMap;
    std::unordered_map<int, int> myHash;

    for (long long i = 0; i < mapSize; i++)
    {
        myMap[i] = i;
        myHash[i] = i;
    }

    std::vector<int> randValues;
    randValues.reserve(accSize);

    std::random_device seed;
    std::mt19937 engine(seed());
    std::uniform_int_distribution<> uniformDist(0, mapSize);
    for (long long i = 0; i < accSize; ++i)
        randValues.push_back(uniformDist(engine));

    auto start = std::chrono::system_clock::now();
    for (long long i = 0; i < accSize; i++)
    {
        myMap[randValues[i]];
    }

    std::chrono::duration<double> dur = std::chrono::system_clock::now() - start;

    std::cout << "time for std::map: " << dur.count() << " seconds" << std::endl;

    auto start2 = std::chrono::system_clock::now();
    for (long long i = 0; i < accSize; ++i)
        myHash[randValues[i]];

    std::chrono::duration<double> dur2 = std::chrono::system_clock::now() - start2;
    std::cout << "time for std::unordered_map: " << dur2.count() << " seconds" << std::endl;

    return 0;
}


[Running] cd "d:\github\TestCpp\" && g++ test1.cpp -o test1 && "d:\github\TestCpp\"test1
time for std::map: 1.10798 seconds
time for std::unordered_map: 0.294019 seconds

[Done] exited with code=0 in 14.13 seconds

https://en.cppreference.com/w/cpp/chrono

'c++' 카테고리의 다른 글

Asan test  (0) 2022.10.29
undef 활용, log message 출력  (0) 2019.10.02
lambda expression on capture  (0) 2018.06.29
Posted by easy16