Advertisement
Stromeczik

topek-vlakna jeho

Dec 16th, 2014
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.86 KB | None | 0 0
  1. #include <math.h>
  2. #include <time.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <windows.h>
  6. #include <iostream>
  7.  
  8.  
  9. using namespace std;
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17. void fillarray(int* pole ,int size)
  18. {
  19.  
  20.         srand(time(NULL));
  21.         cout << "naplneni pole" << endl;
  22.         for(int i=0; i< size ; i++ )
  23.         {
  24.  
  25.                 pole[i] = rand() % 50 ;
  26.  
  27.         }
  28.  
  29.        
  30. }
  31.  
  32. void printarray( int *pole ,int size)
  33. {
  34.  
  35.         for(int i=0; i< size; i++ )
  36.         {
  37.  
  38.                 cout << pole[i] << " ";
  39.  
  40.         }
  41.  
  42.         cout << endl;
  43. }
  44.  
  45.  
  46. int kolik_ms(LPFILETIME pred, LPFILETIME po)
  47. {
  48.    hyper pred64b = pred->dwHighDateTime;
  49.    pred64b = (pred64b << 32) | pred->dwLowDateTime;
  50.    hyper po64b = po->dwHighDateTime;
  51.    po64b = (po64b << 32) | po->dwLowDateTime;
  52.    return (int) ((po64b - pred64b) / 10000);
  53. }
  54.  
  55.  
  56.  
  57. void insertsort(int pole[], int size)
  58. {
  59.         for(int i=0 ;i <size ;i++ )
  60.         {
  61.                  
  62.                  int j = i + 1 ;
  63.                  int tmp = pole[j];
  64.                  while( j > 0 && tmp > pole[j-1])
  65.                  {
  66.                          pole[j] = pole[j-1];
  67.                          j--;
  68.                  }
  69.                  pole[j] = tmp;
  70.                  
  71.  
  72.         }
  73.        
  74.  
  75. }
  76.  
  77.  
  78.  
  79.  
  80. struct struktura{
  81. int *pole;
  82. int velikost;
  83. };
  84.  
  85.  
  86.  
  87.  
  88. DWORD WINAPI vlakno(LPVOID pom)
  89. {
  90.        struktura *tmp = (struktura *)pom;
  91.        insertsort(tmp->pole, tmp->velikost);
  92.        return 0;
  93. }
  94.  
  95.  
  96.  
  97. int main (){
  98.  
  99.  
  100.        
  101.  
  102.  
  103.         cout << " kolik nahodnych cisel chces vygenerovat ?  " << endl;
  104.         cout << " program seradi cisla od nejmensiho po nejvetsi a zmeri cas jaky mu to trvalo" << endl;
  105.  
  106.         int N;
  107.         cin >> N;
  108.  
  109.  
  110.         while (1)
  111.         {
  112.         cout << "kolik jader chces do algoritmu zapojit ? " << endl;
  113.         cout << "maximalni pocet jader zjistis stlacenim ctr + alt + del " << endl;
  114.         cout << "v kolonce historie vyuziti procesoru pocet grafu je pocet jader/vlaken " << endl;
  115.  
  116.         int volba1;
  117.         cin >> volba1;
  118.  
  119.         int * pole = new int[N];
  120.         FILETIME cas_pred, cas_po;
  121.  
  122.         fillarray(pole,N);
  123.        
  124.  
  125.         switch(volba1)
  126.         {
  127.  
  128.         case 1 :
  129.  
  130.                 cout << "trideni pracuje" << endl;
  131.         struktura * struct1 = new struktura;
  132.         struct1->pole = pole ;
  133.         struct1->velikost =N;
  134.  
  135.         GetSystemTimeAsFileTime (&cas_pred);
  136.          HANDLE p1 = CreateThread(0, 0, vlakno, struct1, 0, 0);
  137.           WaitForSingleObject(p1, INFINITE);
  138.           GetSystemTimeAsFileTime (&cas_po);
  139.  
  140.           cout << "cas trideni s "  << volba1 << " jadery a "<< N << " nahodnych cisel trva :" << kolik_ms(&cas_pred , &cas_po) << "ms " << endl << endl;
  141.  
  142.           break;
  143.  
  144.  
  145.         }
  146.  
  147.  
  148.  
  149.        
  150.  
  151.         /*default:
  152.                
  153.                 struktura * struct1 = new struktura;
  154.         struct1->pole = pole ;
  155.         struct1->velikost =N;
  156.  
  157.         GetSystemTimeAsFileTime (&cas_pred);
  158.          HANDLE p1 = CreateThread(0, 0, vlakno, struct1, 0, 0);
  159.           WaitForSingleObject(p1, INFINITE);
  160.           GetSystemTimeAsFileTime (&cas_po);
  161.  
  162.           break;*/
  163.         }
  164.  
  165.  
  166.  
  167.  
  168.         //printarray(pole);
  169.  
  170.  
  171.         //  
  172.         //struktura * struct1 = new struktura;
  173.         //struct1->pole = pole ;
  174.         //struct1->velikost =N/4;
  175.  
  176.         //struktura * struct2 = new struktura;
  177.         //struct2->pole = pole + N/4;
  178.         //struct2->velikost = N/4;
  179.  
  180.         //struktura * struct3 = new struktura;
  181.         //struct3->pole = pole + N/2;
  182.         //struct3->velikost = N/4;
  183.  
  184.         //struktura * struct4 = new struktura;
  185.         //struct4->pole = pole + (3/4 * N);
  186.         //struct4->velikost = N/4;
  187.         //
  188.  
  189.         //cout << "  s vlakny " << endl;
  190.  
  191.  //GetSystemTimeAsFileTime (&cas_pred);
  192.  
  193.  //   HANDLE p1 = CreateThread(0, 0, vlakno, struct1, 0, 0);
  194.         //HANDLE p2 = CreateThread(0, 0, vlakno, struct2, 0, 0);
  195.         //HANDLE p3 = CreateThread(0, 0, vlakno, struct3, 0, 0);
  196.         //HANDLE p4 = CreateThread(0, 0, vlakno, struct4, 0, 0);
  197.  
  198.         // WaitForSingleObject(p1, INFINITE);
  199.         // WaitForSingleObject(p2, INFINITE);
  200.         // WaitForSingleObject(p3, INFINITE);
  201.         // WaitForSingleObject(p4, INFINITE);
  202.  
  203.  //GetSystemTimeAsFileTime (&cas_po);
  204.  
  205.  // //  printarray(pole);
  206.  
  207.  
  208.  
  209.  //
  210.  //  cout << "cas vlakna " << N << " nahodnych cisel trva :" << kolik_ms(&cas_pred , &cas_po) << "ms " << endl;
  211.         //
  212.  //
  213.         // GetSystemTimeAsFileTime (&cas_pred);  
  214.         //// printarray(pole2);
  215.  //    
  216.         //// printarray(pole2);
  217.  
  218.         // GetSystemTimeAsFileTime (&cas_po);
  219.  //
  220.         //  cout << "bez claken : " << N << " nahodnych cisel trva :" << kolik_ms(&cas_pred , &cas_po) << "ms " << endl;
  221.  
  222.  
  223.  
  224.         //  
  225.        
  226.  
  227.    system("pause") ;
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement