Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cmath>
- #include <thread>
- #include <windows.h>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- int flags=0;
- void filler(int start_pos, int end_pos, vector<double> &data)
- {
- for (;start_pos<end_pos; start_pos++)
- {
- data[start_pos]=start_pos;
- for (int i=0; i<400; i++){ //insert some timeconsuming random operations in here
- data[start_pos]=sin(data[start_pos]);
- data[start_pos]=cos(data[start_pos]);}
- }
- flags++;
- }
- int main()
- {
- clock_t begin, end;
- double timer;
- vector<double> data(100000);
- //fill_1(0, 100000, data);
- cout << "Performing 1 thread performance test\n";
- begin=clock();
- thread t(filler,0, 100000, std::ref(data));
- t.detach();
- while(1)
- {
- Sleep(20);
- if (flags==1)
- break;
- }
- end=clock();
- timer=(double)(end-begin)*1000.0 / CLOCKS_PER_SEC;
- data.clear();
- cout << "Test #1 took " << timer << " miliseconds\n";
- flags=0;
- cout << "Performing 2 threads performance test\n";
- begin=clock();
- thread g(filler, 0, 50000, std::ref(data));
- g.detach();
- thread h(filler, 50001, 100000, std::ref(data));
- h.detach();
- while(1)
- {
- Sleep(20);
- if (flags==2)
- break;
- }
- data.clear();
- end=clock();
- timer=(double)(end-begin)*1000.0 / CLOCKS_PER_SEC;
- cout << "Test #2 took " << timer << " miliseconds\n";
- cout << "Performing 4 threads performance test\n";
- begin=clock();
- flags=0;
- thread a(filler,0, 25000, std::ref(data));
- a.detach();
- thread b(filler, 25001, 50000, std::ref(data));
- b.detach();
- thread c(filler, 50001, 75000, std::ref(data));
- c.detach();
- thread d(filler,75001, 100000, std::ref(data));
- d.detach();
- while(1)
- {
- Sleep(20);
- if (flags==4)
- break;
- }
- end=clock();
- timer=(double)(end-begin)*1000.0 / CLOCKS_PER_SEC;
- cout << "Test #3 took " << timer << " miliseconds\n";
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement