Skip to content

Instantly share code, notes, and snippets.

@n-eq
Created April 16, 2017 12:40
Show Gist options
  • Save n-eq/a976efd601fc45320f2c9a6e6141d5f6 to your computer and use it in GitHub Desktop.
Save n-eq/a976efd601fc45320f2c9a6e6141d5f6 to your computer and use it in GitHub Desktop.
Cache illustration
#include <stdio.h>
#include <stdlib.h>
#define N 2000
#define M 1000
int t[M][M] = {{0}};
int main(){
for (int i = 0; i < N; i++)
// swap the two lines to observe the effect
for (int j = 0; j < M; j++)
for (int k = 0; k < M; k++){
// printf("t[%d][%d] @ %p\n", j, k, &(t[j][k]));
t[j][k] = k + j;
}
return EXIT_SUCCESS;
}
@n-eq
Copy link
Author

n-eq commented Apr 16, 2017

gcc -g -O3 -o cachecache cachcecahe.c

Results:

  • 24.7% cache miss versus 99.9% cache miss
  • 3.113s versus 26.991s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment