Advertisement
Guest User

Untitled

a guest
Feb 17th, 2016
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define TEST_SIZE = 70000;
  2.  
  3. // Comparing test1 and test2.
  4.  
  5. // SINGLE-THREAD :
  6. bool test1()
  7. {
  8.     double nonsense = 0;
  9.     for (int i = 0; i < TEST_SIZE; ++i) {
  10.         for (int j = 0; j < TEST_SIZE; ++j) {
  11.             nonsense = sqrt(i + j);
  12.         }
  13.     }
  14.  
  15.     printf("ready");
  16.     return true;
  17. }
  18.  
  19. // MULI-THREAD :
  20. void* first_half(void* ptr)
  21. {
  22.     double nonsense = 0;
  23.     for (int i = 0; i < TEST_SIZE / 2 + 1; ++i) {
  24.         for (int j = 0; j < TEST_SIZE / 2 + 1; ++j) {
  25.             nonsense = sqrt(i + j);
  26.         }
  27.     }
  28. }
  29.  
  30. void* second_half(void* ptr)
  31. {
  32.     double nonsense = 0;
  33.     for (int i = TEST_SIZE / 2 + 1; i < TEST_SIZE; ++i) {
  34.         for (int j = TEST_SIZE / 2 + 1; j < TEST_SIZE; ++j) {
  35.             nonsense = sqrt(i + j);
  36.         }
  37.     }  
  38. }
  39.  
  40. bool test2()
  41. {
  42.     pthread_t t1, t2, t3, t4;
  43.     int ret;
  44.  
  45.     ret = pthread_create(&t1, NULL, first_half, NULL);
  46.     if (ret != 0)
  47.         return false;
  48.     ret = pthread_create(&t2, NULL, second_half, NULL);
  49.     if (ret != 0)
  50.         return false;
  51.  
  52.     // join shortest distance calculation :
  53.     ret = pthread_join(t1, NULL);
  54.     if (ret != 0)
  55.         return false;
  56.  
  57.     // join shortest distance calculation :
  58.     ret = pthread_join(t2, NULL);
  59.     if (ret != 0)
  60.         return false;
  61.  
  62.     printf("ready\n");
  63.     return true;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement