Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <unordered_map>
- #include <random>
- #include <chrono>
- std::unordered_map<std::string, uint32_t> m_players;
- std::unordered_map<uint32_t, std::string> mappedPlayerGuids;
- std::string random_string()
- {
- std::string str("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
- std::random_device rd;
- std::mt19937 generator(rd());
- std::shuffle(str.begin(), str.end(), generator);
- return str.substr(0, 32);
- }
- void fillMap() {
- for (size_t i = 0; i < 1500; i++) {
- std::string playerName = random_string();
- m_players[playerName] = i;
- mappedPlayerGuids[i] = playerName;
- }
- }
- std::string getByFor(uint32_t id) {
- for (const auto& it : m_players) {
- if (it.second == id)
- return it.first;
- }
- return std::string();
- }
- std::string getByFind(uint32_t id) {
- auto it = mappedPlayerGuids.find(id);
- if (it != mappedPlayerGuids.end()) {
- return it->second;
- }
- return std::string();
- }
- std::chrono::high_resolution_clock::time_point time_point;
- uint64_t ns;
- int main()
- {
- fillMap();
- std::cout << m_players.size() << " players size, " << mappedPlayerGuids.size() << " mapped size" << std::endl;
- time_point = std::chrono::high_resolution_clock::now();
- for (size_t i = 1; i < 100000; i++) {
- std::string playerNme = getByFor(static_cast<uint32_t>(std::rand() % i));
- // something
- }
- ns = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now() - time_point).count();
- std::cout << "For Took " << (ns / 1000000) << " ms" << std::endl;
- time_point = std::chrono::high_resolution_clock::now();
- for (size_t i = 1; i < 100000; i++) {
- std::string playerNme = getByFind(static_cast<uint32_t>(std::rand() % i));
- // something
- }
- ns = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now() - time_point).count();
- std::cout << "Find Took " << (ns / 1000000) << " ms" << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement