Advertisement
fsb4000

C++ strings timing

Feb 27th, 2020
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.78 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 <string>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <numeric>
  7. #include <chrono>
  8. #include <random>
  9. #include <iostream>
  10. #include <array>
  11. #include <execution>
  12. #include <mutex>
  13. #include <thread>
  14.  
  15. using namespace std;
  16.  
  17. default_random_engine thread_safe_generate_random_engine()
  18. {
  19.     static mutex local_mutex;
  20.     lock_guard const lock(local_mutex);
  21.  
  22.     static random_device r;
  23.     static default_random_engine gen(r());
  24.     static uniform_int_distribution<unsigned> dist;
  25.  
  26.     return default_random_engine{ dist(gen) };
  27. }
  28.  
  29. string random_str(size_t len)
  30. {
  31.     constexpr array<char, 63> alphabet{ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" };
  32.     thread_local default_random_engine gen = thread_safe_generate_random_engine();
  33.     thread_local uniform_int_distribution<size_t> dis(0, alphabet .size()-2); // оба конца диапазона включаются, не учитываем '\0'
  34.  
  35.     string result;
  36.     result.reserve(len);
  37.     generate_n(
  38.         back_inserter(result),
  39.         len,
  40.         [&]()
  41.         {
  42.             return alphabet.at(dis(gen));
  43.         }
  44.     );
  45.     return result;
  46. }
  47.  
  48. class random_string20 : public string
  49. {
  50. public:
  51.     random_string20() noexcept(noexcept(random_str(20)))
  52.         : string(random_str(20))
  53.     {}
  54. };
  55.  
  56. void update(vector<string>& v)
  57. {
  58.     constexpr array<char, 63> alphabet{ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" };
  59.     thread_local default_random_engine gen = thread_safe_generate_random_engine();
  60.     thread_local uniform_int_distribution<size_t> dis(0, alphabet.size() - 2); // оба конца диапазона включаются, не учитываем '\0'
  61.  
  62.     for_each(std::execution::par,
  63.         v.begin(), v.end(),
  64.         [&](auto& s)
  65.         {
  66.             for_each(s.begin(), s.end(),
  67.                 [&](auto& c)
  68.                 {
  69.                     c = alphabet.at(dis(gen));
  70.                 }
  71.             );
  72.         }
  73.     );
  74. }
  75.  
  76. size_t test(vector<string>& vector_string100)
  77. {
  78.     // как будут готовы range, заменить на ranges
  79.     array<int, 1000> const range{};
  80.     auto const begin = chrono::high_resolution_clock::now();
  81.     update(vector_string100);
  82.     auto const end = chrono::high_resolution_clock::now();
  83.     cout << vector_string100.size() << " strings generation time = "
  84.         << chrono::duration_cast<chrono::milliseconds>
  85.         (end - begin).count() << '\n';
  86.     return reduce(
  87.         std::execution::par,
  88.         range.begin(), range.end(),
  89.         0,
  90.         [&](int acc, int) {
  91.             random_string20 const s20;
  92.             bool const found = any_of(
  93.                 vector_string100.cbegin(), vector_string100.cend(),
  94.                 [&](string const& str100)
  95.                 {
  96.                     return str100.find(s20) != string::npos;
  97.                 }
  98.             );
  99.             return found ? acc + 1 : acc;
  100.         }
  101.     );
  102. }
  103.  
  104. int main()
  105. {
  106.     auto begin = chrono::high_resolution_clock::now();
  107.     vector<string> vector_string100(100000, string(100, ' '));
  108.     auto end = chrono::high_resolution_clock::now();
  109.     cout << "Memory allocation time: " <<
  110.         chrono::duration_cast<chrono::milliseconds>
  111.         (end - begin).count() << '\n';
  112.     for (int i = 0; i < 10; ++i)
  113.     {
  114.         begin = chrono::high_resolution_clock::now();
  115.         size_t const matches = test(vector_string100);
  116.         end = chrono::high_resolution_clock::now();
  117.         cout << matches << " strings matches, time = "
  118.             << chrono::duration_cast<chrono::milliseconds>
  119.             (end - begin).count() << '\n';
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement