Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Cache lines demo program --- notice the
- * the difference in runtime when accessing
- * the matrix row-wise / column-wise.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #define N 30000
- int main(int argc, char *argv[])
- {
- int i, j, sum = 0;
- if (argc < 2) { printf("Please supply 0 or 1 as an argument.\n"); return 1; }
- int** a = (int**) malloc(sizeof(int*) * N);
- for (i = 0; i < N; i++) {
- a[i] = (int*) malloc(sizeof(int) * N);
- }
- if (atoi(argv[1])) {
- for (j = 0; j < N; j++) {
- for (i = 0; i < N; i++) {
- sum += a[i][j];
- }
- }
- } else {
- for (i = 0; i < N; i++) {
- for (j = 0; j < N; j++) {
- sum += a[i][j];
- }
- }
- }
- printf("sum: %d\n", sum);
- for (i = 0; i < N; i++) {
- free(a[i]);
- }
- free(a);
- }
Advertisement
Add Comment
Please, Sign In to add comment