SHARE
TWEET

Untitled

a guest Jan 15th, 2020 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <unordered_map>
  3. #include <random>
  4. #include <chrono>
  5.  
  6. std::unordered_map<std::string, uint32_t> m_players;
  7. std::unordered_map<uint32_t, std::string> mappedPlayerGuids;
  8.  
  9. std::string random_string()
  10. {
  11.     std::string str("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
  12.     std::random_device rd;
  13.     std::mt19937 generator(rd());
  14.     std::shuffle(str.begin(), str.end(), generator);
  15.     return str.substr(0, 32);
  16. }
  17.  
  18. void fillMap() {
  19.     for (size_t i = 0; i < 1500; i++) {
  20.         std::string playerName = random_string();
  21.         m_players[playerName] = i;
  22.         mappedPlayerGuids[i] = playerName;
  23.     }
  24. }
  25.  
  26. std::string getByFor(uint32_t id) {
  27.     for (const auto& it : m_players) {
  28.         if (it.second == id)
  29.             return it.first;
  30.     }
  31.     return std::string();
  32. }
  33.  
  34. std::string getByFind(uint32_t id) {
  35.     auto it = mappedPlayerGuids.find(id);
  36.     if (it != mappedPlayerGuids.end()) {
  37.         return it->second;
  38.     }
  39.     return std::string();
  40. }
  41.  
  42. std::chrono::high_resolution_clock::time_point time_point;
  43. uint64_t ns;
  44.  
  45. int main()
  46. {
  47.     fillMap();
  48.     std::cout << m_players.size() << " players size, " << mappedPlayerGuids.size() << " mapped size" << std::endl;
  49.  
  50.     time_point = std::chrono::high_resolution_clock::now();
  51.     for (size_t i = 1; i < 100000; i++) {
  52.         std::string playerNme = getByFor(static_cast<uint32_t>(std::rand() % i));
  53.         // something
  54.     }
  55.  
  56.  
  57.     ns = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now() - time_point).count();
  58.     std::cout << "For Took " << (ns / 1000000) << " ms" << std::endl;
  59.     time_point = std::chrono::high_resolution_clock::now();
  60.  
  61.     for (size_t i = 1; i < 100000; i++) {
  62.         std::string playerNme = getByFind(static_cast<uint32_t>(std::rand() % i));
  63.         // something
  64.     }
  65.  
  66.     ns = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now() - time_point).count();
  67.     std::cout << "Find Took " << (ns / 1000000) << " ms" << std::endl;
  68. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top