-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_size.c
37 lines (32 loc) · 841 Bytes
/
cache_size.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[])
{
clock_t t1, t2;
int cache_line = atoi(argv[1]);
int arr_size = atoi(argv[2]);
int loops = atoi(argv[3]);
int *arr;
arr = malloc(arr_size * 1024);
int lenMod = (arr_size*1024)/(sizeof *arr) - 1;
if (arr != NULL) {
int steps = 128*1024*1024;
int i, j;
t1 = clock();
for(i=0; i < loops; ++i) {
for(j=0; j < steps; ++j) {
arr[(j*cache_line) & lenMod]++;
}
}
t2 = clock();
free(arr);
arr = NULL;
double diff = ((double)t2 - (double)t1)/CLOCKS_PER_SEC/loops;
printf("%fs per loop\n", diff);
} else {
printf("Failed to allocate memory!");
return 1;
}
return 0;
}