Skip to content

Instantly share code, notes, and snippets.

@jvns
Last active January 1, 2016 06:59
Show Gist options
  • Save jvns/8108844 to your computer and use it in GitHub Desktop.
Save jvns/8108844 to your computer and use it in GitHub Desktop.
Trying to demonstrate that I have a CPU cache
/**
* The way this works:
* ./cache array_size n_repeats allocates an array of size 32 *
* array_size, and * adds up all the numbers in it n_repeats times.
*
* Try for example
* $ time ./cache 1 3200000
* $ time ./cache 3200000 1
*
* This was supposed to demonstrate that I have a CPU cache, but it does
* not =(
*/
#include <stdio.h>
#include <stdlib.h>
#define BLOCK_SIZE 32
int main(int argc, char** argv) {
if (argc < 3) {
printf("Usage: ./cache array_size num_repeats\n");
exit(0);
}
int array_size = atoi(argv[1]) * BLOCK_SIZE;
int n_repeats = atoi(argv[2]);
char* chars = malloc(sizeof(char) * array_size);
// Initialize our array
int i,k;
int total = 0;
for (k = 0; k < n_repeats; k++) {
for (i = 0; i < array_size; i++) {
chars[i] = rand();
}
for (i = 0; i < array_size; i++) {
total += chars[i];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment