Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <thread>
  3. #include <vector>
  4. #include <mutex>
  5. #include <chrono>
  6. #include <algorithm>
  7. //#define PARAL
  8. //#define SEQ
  9.  
  10. std::mutex mut;
  11.  
  12. void worker(std::vector<int>& vct, int q, int pos, char thr_num)
  13. {
  14. for (int i = pos; i < pos + q; ++i)
  15. {
  16. if (vct[i] % 2 != 0)
  17. vct[i] = 0;
  18. }
  19. }
  20.  
  21. int main(int argc, char* argv[])
  22. {
  23. const auto TEST_SIZE = 100000000;
  24. std::vector<int> result(TEST_SIZE);
  25. std::generate(result.begin(),result.end(),std::rand);
  26. auto beg = std::chrono::steady_clock::now();
  27.  
  28. #ifdef PARAL
  29. int quart = result.size() / std::thread::hardware_concurrency();
  30. std::vector<std::thread> threads;
  31. for (size_t i = 0; i < std::thread::hardware_concurrency();++i) {
  32. threads.emplace_back(worker,std::ref(result),quart,quart*i,i);
  33. }
  34. for (auto && it : threads)
  35. it.join();
  36. #elif defined (SEQ)
  37. worker(result,result.size(),0,0);
  38. #endif
  39. std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - beg).count();
  40. return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement