shushus

Спринт 9. Ускоряем for_each. Задание 1.

Mar 6th, 2023 (edited)
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. #include <algorithm>
  2. #include <execution>
  3. #include <iostream>
  4. #include <list>
  5. #include <random>
  6. #include <string>
  7. #include <string_view>
  8. #include <type_traits>
  9. #include <vector>
  10. #include <future>
  11.  
  12. #include "log_duration.h"
  13.  
  14. using namespace std;
  15.  
  16. string GenerateWord(mt19937& generator, int max_length) {
  17.     const int length = uniform_int_distribution(1, max_length)(generator);
  18.     string word;
  19.     word.reserve(length);
  20.     for (int i = 0; i < length; ++i) {
  21.         word.push_back(uniform_int_distribution('a', 'z')(generator));
  22.     }
  23.     return word;
  24. }
  25.  
  26. template <template <typename> typename Container>
  27. Container<string> GenerateDictionary(mt19937& generator, int word_count, int max_length) {
  28.     vector<string> words;
  29.     words.reserve(word_count);
  30.     for (int i = 0; i < word_count; ++i) {
  31.         words.push_back(GenerateWord(generator, max_length));
  32.     }
  33.     return Container(words.begin(), words.end());
  34. }
  35.  
  36. struct Reverser {
  37.     void operator()(string& value) const {
  38.         reverse(value.begin(), value.end());
  39.     }
  40. };
  41.  
  42. template <typename Container, typename Function>
  43. void Test(string_view mark, Container keys, Function function) {
  44.     LOG_DURATION(mark);
  45.     function(keys, Reverser{});
  46. }
  47.  
  48. #define TEST(function) Test(#function, keys, function<remove_const_t<decltype(keys)>, Reverser>)
  49.  
  50. template <typename ForwardRange, typename Function>
  51. void ForEach(ForwardRange& range, Function function) {
  52.     int parts = 4;
  53.     auto part_size = range.size() / parts;
  54.     auto part_begin = range.begin();
  55.     auto part_end = next(part_begin, part_size);
  56.  
  57.     vector<future<void>> result;
  58.     for (int i = 0; i < parts; ++i, part_begin = part_end,
  59.                                     part_end = (i == parts - 1
  60.                                                 ? range.end()
  61.                                                 : next(part_begin, part_size))) {
  62.         result.push_back(async([function,
  63.                                 part_begin,
  64.                                 part_end] {
  65.             for_each(std::execution::par,
  66.                      part_begin,
  67.                      part_end,
  68.                      function);}));
  69.     }
  70. }
  71.  
  72. int main() {
  73.     vector<string> strings = {"cat", "dog", "code"};
  74.  
  75.     ForEach(strings, [](string& s) {
  76.         reverse(s.begin(), s.end());
  77.     });
  78.  
  79.     for (string_view s : strings) {
  80.         cout << s << " ";
  81.     }
  82.     cout << endl;
  83.  
  84.     mt19937 generator;
  85.     const auto keys = GenerateDictionary<list>(generator, 50'000, 5'000);
  86.  
  87.     TEST(ForEach);
  88.  
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment