Advertisement
UrQuan

NAR - 1 i 2

Apr 11th, 2016
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3. #include <thread>
  4. #include <math.h>
  5. #include <chrono>
  6. #include <stdlib.h>     /* srand, rand */
  7. #include <time.h>       /* time */
  8. typedef std::chrono::high_resolution_clock Clock;
  9.  
  10. using namespace std;
  11.  
  12. class Timer
  13. {
  14. public:
  15.     Timer() : beg_(clock_::now()) {}
  16.     void reset() { beg_ = clock_::now(); }
  17.     double elapsed() const {
  18.         return std::chrono::duration_cast<std::chrono::microseconds>
  19.             (clock_::now() - beg_).count();
  20.     }
  21.  
  22. private:
  23.     typedef std::chrono::high_resolution_clock clock_;
  24.     typedef std::chrono::duration<double, std::ratio<1> > second_;
  25.     std::chrono::time_point<clock_> beg_;
  26. };
  27.  
  28. long Prosti(int n)
  29. {
  30.     int count=0;
  31.     long a = 2;
  32.     while(count<n)
  33.     {
  34.         long b = 2;
  35.         int prime = 1;// to check if found a prime
  36.         while(b * b <= a)
  37.         {
  38.             if(a % b == 0)
  39.             {
  40.                 prime = 0;
  41.                 break;
  42.             }
  43.             b++;
  44.         }
  45.         if(prime > 0)
  46.         count++;
  47.         a++;
  48.     }
  49.     return (--a);
  50. }
  51.  
  52. void sporo(int n, int br)
  53. {
  54.     cout << "thread T" << br << " started" << endl;
  55.     long x = Prosti(n);
  56.     cout << "thread T" << br << " ended with: " << x << endl;
  57. }
  58.  
  59. int main(int argc, char* argv[])
  60. {
  61.     int threadNumber = 2;
  62.     if( argc >= 2){
  63.         threadNumber = atoi(argv[1]);
  64.         if( threadNumber < 1 || threadNumber >64 ) return 0;
  65.         cout << "Zeljeni broj threadova: " << threadNumber << endl;
  66.     }
  67.     int n = 5000;
  68.     if( argc >= 3){
  69.         n = atoi(argv[2]);
  70.         if( n < 1) return 0;
  71.         cout << "Zeljeni prosti broj: " << n << endl;
  72.     }
  73.     double t1, t2;
  74.     double r1, r2;
  75.     Timer tmr;
  76.     thread p[128];
  77.  
  78. // sekvencijalna izvedba X threadova
  79.  
  80.     t1 = tmr.elapsed();
  81.     cout << endl << "Prvo vrijeme: " << t1 << endl;
  82.     for( int z = 0; z < threadNumber; z++){
  83.         p[z] = std::thread(sporo, std::ref(n), z);
  84.         p[z].join();
  85.     }
  86.     t2 = tmr.elapsed();
  87.     cout << "Drugo vrijeme: " << t2 << endl;
  88.     std::cout << "SEKVENCIJALNO: Proteklo vrijeme (razlika T2-T1): " << t2-t1 << std::endl;
  89.     r1 = t2-t1;
  90. // paralelna izvedba X threadova
  91.  
  92.     t1 = tmr.elapsed();
  93.     cout << endl << "Prvo vrijeme: " << t1 << endl;
  94.     for( int z = threadNumber; z < threadNumber*2; z++){
  95.         p[z] = std::thread(sporo, std::ref(n), z);
  96.     }
  97.  
  98.     for( int z = threadNumber; z < threadNumber*2; z++){
  99.         p[z].join();
  100.     }
  101.     t2 = tmr.elapsed();
  102.     cout << "Drugo vrijeme: " << t2 << endl;
  103.     std::cout << "PARALELNO: Proteklo vrijeme (razlika T2-T1): " << t2-t1 << std::endl;
  104.     r2 = t2-t1;
  105. // Izvjesce:
  106.     cout << endl << endl << "Paralelno je brze, i to za: " << r1-r2 << endl;
  107.     cout << "Sto iznosi: " << (int) ((r2/r1)*100) << "%" << endl;
  108.     cout << "Broj threadova: " << threadNumber << endl;
  109.     cout << "Racunajuci prosti broj: " << n << endl;
  110.  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement