shushus

Спринт 9. Параллельность на каждый день.

Mar 6th, 2023 (edited)
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <random>
  4. #include <string>
  5. #include <string_view>
  6. #include <execution>
  7.  
  8. #include "log_duration.h"
  9.  
  10. using namespace std;
  11.  
  12. string GenerateQuery(mt19937& generator, int max_length, int space_rate) {
  13.     const int length = uniform_int_distribution(1, max_length)(generator);
  14.     string query(length, ' ');
  15.     for (char& c : query) {
  16.         const int rnd = uniform_int_distribution(0, space_rate - 1)(generator);
  17.         if (rnd > 0) {
  18.             c = 'a' + (rnd - 1);
  19.         }
  20.     }
  21.     return query;
  22. }
  23.  
  24. template <typename Solver>
  25. void Test(string_view mark, string_view s, Solver solver) {
  26.     int result;
  27.     {
  28.         LOG_DURATION(mark);
  29.         result = solver(s);
  30.     }
  31.     cout << result << endl;
  32. }
  33.  
  34. #define TEST(solver) Test(#solver, s, solver)
  35.  
  36. int CountWords(string_view str) {
  37.     return count(execution::par, str.begin(), str.end(), ' ') + 1;
  38. }
  39.  
  40. int main() {
  41.     cout << CountWords("pretty little octopus"sv) << endl;
  42.  
  43.     mt19937 generator;
  44.  
  45.     const string s = GenerateQuery(generator, 50'000'000, 4);
  46.  
  47.     TEST(CountWords);
  48.  
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment