#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