Advertisement
mahmoodn

mm_int

Mar 5th, 2022
1,067
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define N 3200
  6. void multiplyMatrices(int first[][N], int second[][N], int result[][N])
  7. {
  8.    // Multiplying first and second matrices and storing it in result
  9.    for (int i = 0; i < N; ++i) {
  10.       for (int j = 0; j < N; ++j) {
  11.          for (int k = 0; k < N; ++k) {
  12.             result[i][j] += first[i][k] * second[k][j];
  13.          }
  14.       }
  15.    }
  16. }
  17.  
  18. void fill_matrices(int first[][N], int second[][N], int result[][N])
  19. {
  20.     srand(time(NULL)); // randomize seed
  21.     for (int i = 0; i < N; i++){
  22.         for (int j = 0; j < N; j++){
  23.             first[i][j] = rand() % 10;
  24.             second[i][j] = rand() % 10;
  25.             result[i][j] = 0;
  26.         }
  27.     }
  28. }
  29. void print(int first[][N], int second[][N], int result[][N])
  30. {
  31.     srand48(time(NULL)); // randomize seed
  32.     printf("First:\n");
  33.     for (int i = 0; i <  N; i++){
  34.         printf("[ ");
  35.         for (int j = 0; j < N; j++){
  36.             printf("%d ", first[i][j]);
  37.         }
  38.         printf("]\n");
  39.     }
  40.     printf("\nSecond:\n");
  41.     for (int i = 0; i <  N; i++){
  42.         printf("[ ");
  43.         for (int j = 0; j < N; j++){
  44.             printf("%d ", second[i][j]);
  45.         }
  46.         printf("]\n");
  47.     }
  48.     printf("\nResult:\n");
  49.     for (int i = 0; i <  N; i++){
  50.         printf("[ ");
  51.         for (int j = 0; j < N; j++){
  52.             printf("%d ", result[i][j]);
  53.         }
  54.         printf("]\n");
  55.     }  
  56. }
  57.  
  58. int main()
  59. {
  60.     clock_t start1, start2, stop1, stop2; // declaration of ftype
  61.     double execution_time1,execution_time2;
  62.  
  63.     int first[N][N], second[N][N], result[N][N];
  64.    
  65.     start1 = clock();
  66.     fill_matrices(first, second, result);
  67.     stop1 = clock();
  68.    
  69.    
  70.     start2 = clock();
  71.     multiplyMatrices(first, second, result);
  72.     stop2 = clock();
  73.  
  74.     execution_time2 = ((double) (stop2 - start2)) / CLOCKS_PER_SEC;
  75.     execution_time1 = ((double) (stop2 - start1)) / CLOCKS_PER_SEC;
  76.     //print(first, second, result);
  77.     printf("Total execution Time in seconds: %.10lf\n", execution_time1 );
  78.     printf("MM execution Time in seconds: %.10lf\n", execution_time2 );
  79.     return 0;
  80. }
  81.    
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement