Advertisement
dark-Matter

seq.cpp

Mar 7th, 2021
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.81 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <chrono>
  3.  
  4. using namespace std;
  5. using namespace std::chrono;
  6.  
  7. int MAX = 100;
  8. int n;
  9.  
  10. void print_array(int **a) {
  11.     for (int i = 0; i < n; i++)
  12.     {
  13.         for (int j = 0; j < n; j++)
  14.         {
  15.             cout << a[i][j] << " ";
  16.         }
  17.         cout << endl;
  18.     }
  19.    
  20. }
  21.  
  22. milliseconds job( int n)
  23. {
  24.     int **a, **b, **c;
  25.     a = (int**)malloc(n*sizeof(int*));
  26.     b = (int**)malloc(n*sizeof(int*));
  27.     c = (int**)malloc(n*sizeof(int*));
  28.     for (int i = 0; i < n; i++)
  29.     {
  30.         int *x = (int*)malloc(n*sizeof(int));
  31.         for (int j = 0; j < n; j++)
  32.         {
  33.             x[j] = rand()%100;
  34.         }
  35.         a[i] = x;
  36.     }
  37.  
  38.     for (int i = 0; i < n; i++)
  39.     {
  40.         int *x = (int*)malloc(n*sizeof(int));
  41.         int *y = (int*)malloc(n*sizeof(int));
  42.         for (int j = 0; j < n; j++)
  43.         {
  44.             x[j] = rand()%100;
  45.             y[j] = rand()%100;
  46.         }
  47.         b[i] = x;
  48.         c[i] = y;
  49.     }
  50.    
  51.    // print_array(a);
  52.    // print_array(b);
  53.     clock_t tic = clock();
  54.     auto strt = duration_cast< milliseconds >(system_clock::now().time_since_epoch());
  55.     for (int i = 0; i < n; i++)
  56.     {
  57.         for (int j = 0; j < n; j++)
  58.         {
  59.             c[i][j]=0;
  60.             for (int k = 0; k < n; k++)
  61.             {
  62.                 c[i][j] += a[i][k]*b[k][j];
  63.             }
  64.            
  65.         }
  66.     }
  67.     auto stop = duration_cast< milliseconds >(system_clock::now().time_since_epoch());
  68.     clock_t tac = clock();
  69.     //printf("Elapsed: %f seconds\n", (double)(tac - tic) / CLOCKS_PER_SEC);
  70.     //print_array(c);
  71.     free(a);
  72.     free(b);
  73.     free(c);
  74.     return stop-strt;
  75. }
  76.  
  77. int main(int argc, char** argv)
  78. {
  79.     int n = atoi(argv[1]);
  80.     cout << "n : " << n << "time: " << endl;
  81.     job(n);
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement