Ghytro

accumulate1.cpp

Feb 27th, 2021
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <thread>
  4. #include <ctime>
  5. #include <cstdlib>
  6. #include <chrono>
  7.  
  8. std::vector<int> gen_rand_vector(const size_t &size, const int &max_value)
  9. {
  10.     std::srand(std::time(NULL));
  11.     std::vector<int> ret_value;
  12.     for (size_t i = 0; i < size; ++i)
  13.         ret_value.push_back(std::rand() % max_value);
  14.     return ret_value;
  15. }
  16.  
  17. long long accumulate_singlethread(const std::vector<int> &vector)
  18. {
  19.     //std::cout << "Linear start\n";
  20.     register long long sum = 0;
  21.     for (int i = 0; i < vector.size(); ++i)
  22.         sum += vector[i];
  23.     return sum;
  24. }
  25.  
  26. long long accumulate_multithread(const std::vector<int> &vector, const int &threads_num)
  27. {
  28.     //std::cout << "Linear stopped\n";
  29.     auto accumulate_interval = [&threads_num](const std::vector<int> &vector, int start, long long &ans)
  30.     {
  31.         //std::cout << "2\n";
  32.         register long long result = 0;
  33.         for (size_t i = start; i < vector.size(); i += threads_num)
  34.             result += vector[i];
  35.         ans = result;
  36.     };
  37.  
  38.     std::vector<long long> temp_answers(threads_num);
  39.     std::vector<std::thread> workers;
  40.     for (int i = 0; i < threads_num; ++i)
  41.     {
  42.         //std::cout << "1\n";
  43.         workers.push_back(std::thread(accumulate_interval, std::cref(vector), i, std::ref(temp_answers[i])));
  44.     }
  45.     long long total_answer = 0;
  46.     for (int i = 0; i < threads_num; ++i)
  47.     {
  48.         workers[i].join();
  49.         //std::cout << "3\n";
  50.         total_answer += temp_answers[i];
  51.     }
  52.  
  53.     return total_answer;
  54. }
  55.  
  56. int main()
  57. {
  58.     std::cout << "Creating large vector...\n";
  59.     std::vector<int> large_vector = gen_rand_vector(1e9, 1e6);
  60.  
  61.     std::cout << "Exectuting singlethread...\n";
  62.     auto start_time = std::chrono::high_resolution_clock::now();
  63.     long long answer_singlethread = accumulate_singlethread(large_vector);
  64.     auto singlethread_duration = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - start_time);
  65.  
  66.     std::cout << "Executing multithread...\n";
  67.     start_time = std::chrono::high_resolution_clock::now();
  68.     long long answer_multithread = accumulate_multithread(large_vector, 4);
  69.     auto multithread_duration = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - start_time);
  70.  
  71.     std::cout << "Answer for singlethread: " << answer_singlethread << std::endl <<
  72.                  "Answer for multithread: " << answer_multithread << std::endl <<
  73.                  "Singlethread duration: " << singlethread_duration.count() / 1000.0 << "s" << std::endl <<
  74.                  "Multithread duration: " << multithread_duration.count() / 1000.0 << "s" << std::endl;
  75.     return 0;
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment