Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2014
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <thread>
  5. #include <windows.h>
  6. #include <cstdlib>
  7. #include <ctime>
  8. using namespace std;
  9. int flags=0;
  10.  
  11. void filler(int start_pos, int end_pos, vector<double> &data)
  12. {
  13. for (;start_pos<end_pos; start_pos++)
  14.     {
  15.     data[start_pos]=start_pos;
  16.     for (int i=0; i<400; i++){ //insert some timeconsuming random operations in here
  17.     data[start_pos]=sin(data[start_pos]);
  18.     data[start_pos]=cos(data[start_pos]);}
  19.     }
  20.     flags++;
  21. }
  22.  
  23.  
  24. int main()
  25. {
  26.     clock_t begin, end;
  27.     double timer;
  28.     vector<double> data(100000);
  29.     //fill_1(0, 100000, data);
  30.     cout << "Performing 1 thread performance test\n";
  31.     begin=clock();
  32.     thread t(filler,0, 100000, std::ref(data));
  33.     t.detach();
  34.     while(1)
  35.     {
  36.         Sleep(20);
  37.         if (flags==1)
  38.             break;
  39.     }
  40.     end=clock();
  41.     timer=(double)(end-begin)*1000.0 / CLOCKS_PER_SEC;
  42.     data.clear();
  43.     cout << "Test #1 took " << timer << " miliseconds\n";
  44.     flags=0;
  45.     cout << "Performing 2 threads performance test\n";
  46.     begin=clock();
  47.     thread g(filler, 0, 50000, std::ref(data));
  48.     g.detach();
  49.     thread h(filler, 50001, 100000, std::ref(data));
  50.     h.detach();
  51.         while(1)
  52.     {
  53.         Sleep(20);
  54.         if (flags==2)
  55.             break;
  56.     }
  57.     data.clear();
  58.     end=clock();
  59.     timer=(double)(end-begin)*1000.0 / CLOCKS_PER_SEC;
  60.     cout << "Test #2 took " << timer << " miliseconds\n";
  61.     cout << "Performing 4 threads performance test\n";
  62.     begin=clock();
  63.     flags=0;
  64.     thread a(filler,0, 25000, std::ref(data));
  65.     a.detach();
  66.     thread b(filler, 25001, 50000, std::ref(data));
  67.     b.detach();
  68.     thread c(filler, 50001, 75000, std::ref(data));
  69.     c.detach();
  70.     thread d(filler,75001, 100000, std::ref(data));
  71.     d.detach();
  72.            while(1)
  73.     {
  74.         Sleep(20);
  75.         if (flags==4)
  76.             break;
  77.     }
  78.      end=clock();
  79.     timer=(double)(end-begin)*1000.0 / CLOCKS_PER_SEC;
  80.     cout << "Test #3 took " << timer << " miliseconds\n";
  81.     system("PAUSE");
  82.     return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement