Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <thread>
- #include <vector>
- #include <mutex>
- #include <chrono>
- #include <algorithm>
- //#define PARAL
- //#define SEQ
- std::mutex mut;
- void worker(std::vector<int>& vct, int q, int pos, char thr_num)
- {
- for (int i = pos; i < pos + q; ++i)
- {
- if (vct[i] % 2 != 0)
- vct[i] = 0;
- }
- }
- int main(int argc, char* argv[])
- {
- const auto TEST_SIZE = 100000000;
- std::vector<int> result(TEST_SIZE);
- std::generate(result.begin(),result.end(),std::rand);
- auto beg = std::chrono::steady_clock::now();
- #ifdef PARAL
- int quart = result.size() / std::thread::hardware_concurrency();
- std::vector<std::thread> threads;
- for (size_t i = 0; i < std::thread::hardware_concurrency();++i) {
- threads.emplace_back(worker,std::ref(result),quart,quart*i,i);
- }
- for (auto && it : threads)
- it.join();
- #elif defined (SEQ)
- worker(result,result.size(),0,0);
- #endif
- std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - beg).count();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement