Advertisement
fsb4000

Cpp

Feb 12th, 2020
611
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.18 KB | None | 0 0
  1. // This is a personal academic project. Dear PVS-Studio, please check it.
  2. // PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
  3. #include <numeric>
  4. #include <algorithm>
  5. #include <string>
  6. #include <vector>
  7. #include <chrono>
  8. #include <random>
  9. #include <iostream>
  10. #include <array>
  11. #include <mutex>
  12. #include <thread>
  13. #include <unordered_set>
  14. #include <string_view>
  15. #include <cassert>
  16.  
  17. using namespace std;
  18.  
  19. void update_str(string& s)
  20. {
  21.     constexpr array<char, 63> alphabet{ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" };
  22.     thread_local random_device r;
  23.     thread_local default_random_engine gen(r());
  24.     thread_local uniform_int_distribution<size_t> dis(0, alphabet.size() - 2); // оба конца диапазона включаются, не учитываем '\0'
  25.  
  26.     for(auto&& c: s)
  27.     {
  28.         c = alphabet.at(dis(gen));
  29.     }
  30. }
  31.  
  32. class random_string20 : public string
  33. {
  34. public:
  35.     random_string20()
  36.     {
  37.         resize(20);
  38.         update_str(*this);
  39.     }
  40. };
  41.  
  42.  
  43. void update_vector(vector<string>& v)
  44. {
  45.     for_each(
  46.         v.begin(), v.end(),
  47.         [](string& s)
  48.         {
  49.             update_str(s);
  50.         }
  51.     );
  52. }
  53.  
  54. size_t test(vector<string>& vector_string100)
  55. {
  56.     auto result = size_t(0);
  57.     auto begin = chrono::high_resolution_clock::now();
  58.     update_vector(vector_string100);
  59.     auto end = chrono::high_resolution_clock::now();
  60.     {
  61.         cout << "String generation time: "
  62.             << chrono::duration_cast<chrono::milliseconds>
  63.             (end - begin).count() << '\n';
  64.         begin = chrono::high_resolution_clock::now();
  65.         unordered_set<string_view> hash_set;
  66.         hash_set.reserve(81 * vector_string100.size());
  67.         for (auto&& s : vector_string100)
  68.         {
  69.             assert(s.size() >= 19);
  70.             for (size_t i = 0; i < s.size() - 19; ++i)
  71.             {
  72.                 hash_set.insert(string_view(&s.at(i), 20));
  73.             }
  74.         }
  75.         end = chrono::high_resolution_clock::now();
  76.         cout << "Hash generation time: "
  77.             << chrono::duration_cast<chrono::milliseconds>
  78.             (end - begin).count() << '\n';
  79.         begin = chrono::high_resolution_clock::now();
  80.         // как будут готовы range, заменить на ranges
  81.         array<int, 1000> const range{};
  82.         result = accumulate(
  83.             range.begin(), range.end(),
  84.             size_t(0),
  85.             [&](size_t acc, size_t)
  86.             {
  87.                 random_string20 const s20;
  88.                 auto const iter = hash_set.find(s20);
  89.                 bool const found = (iter != hash_set.end());
  90.                 return found ? acc + 1 : acc;
  91.             }
  92.         );
  93.         end = chrono::high_resolution_clock::now();
  94.         cout << "Substring search time: "
  95.             << chrono::duration_cast<chrono::milliseconds>
  96.             (end - begin).count() << '\n';
  97.         begin = chrono::high_resolution_clock::now();
  98.     }
  99.     end = chrono::high_resolution_clock::now();
  100.     cout << "Hashmap destructor time: "
  101.         << chrono::duration_cast<chrono::milliseconds>
  102.         (end - begin).count() << '\n';
  103.     return result;
  104. }
  105.  
  106. int main()
  107. {
  108.     for (int i = 0; i < 10; ++i)
  109.     {
  110.         auto const begin = chrono::high_resolution_clock::now();
  111.         vector<string> vector_string100(100000, string(100, ' '));
  112.         size_t const matches = test(vector_string100);
  113.         auto const end = chrono::high_resolution_clock::now();
  114.         cout << matches << " strings matches, time = "
  115.             << chrono::duration_cast<chrono::milliseconds>
  116.             (end - begin).count() << "\n\n\n";
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement