MrSlippery

Cache lines

Jan 26th, 2017
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.85 KB | None | 0 0
  1. /**
  2.  * Cache lines demo program --- notice the
  3.  * the difference in runtime when accessing
  4.  * the matrix row-wise / column-wise.
  5.  */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8.  
  9. #define N 30000
  10.  
  11. int main(int argc, char *argv[])
  12. {
  13.     int i, j, sum = 0;
  14.     if (argc < 2) { printf("Please supply 0 or 1 as an argument.\n"); return 1; }
  15.     int** a = (int**) malloc(sizeof(int*) * N);
  16.     for (i = 0; i < N; i++) {
  17.         a[i] = (int*) malloc(sizeof(int) * N);
  18.     }
  19.  
  20.     if (atoi(argv[1])) {
  21.         for (j = 0; j < N; j++) {
  22.             for (i = 0; i < N; i++) {
  23.                 sum += a[i][j];
  24.             }
  25.         }
  26.     } else {
  27.         for (i = 0; i < N; i++) {
  28.             for (j = 0; j < N; j++) {
  29.                 sum += a[i][j];
  30.             }
  31.         }
  32.     }
  33.     printf("sum: %d\n", sum);
  34.  
  35.     for (i = 0; i < N; i++) {
  36.         free(a[i]);
  37.     }
  38.     free(a);
  39. }
Advertisement
Add Comment
Please, Sign In to add comment