Commit 9b5553a5a46045658faf15680d4272f8e446fd84
1 parent
51366efbad
Exists in
master
Memory access time
Showing 1 changed file with 45 additions and 0 deletions Side-by-side Diff
memory_access_time.c
View file @
9b5553a
1 | +#include<stdio.h> | |
2 | +#include <sys/time.h> | |
3 | +#include <stdlib.h> | |
4 | + | |
5 | +#define KB 1024 | |
6 | +#define MB 1024 * KB | |
7 | +#define MAX_ARRAY_SIZE 32*MB | |
8 | +#define STRIDE_SIZE 16 | |
9 | +#define CPU_FREQ 900 | |
10 | + | |
11 | +typedef unsigned long long int u64; | |
12 | + | |
13 | +u64 access_time_1(u64 size, int stride_size){ | |
14 | + struct timeval start, end; | |
15 | + struct timezone tz; | |
16 | + | |
17 | + int *array = (int *)malloc(size); | |
18 | + int len = (size)/sizeof(int); | |
19 | + unsigned int i; | |
20 | + | |
21 | + gettimeofday(&start, &tz); | |
22 | + for(i=0; i < MAX_ARRAY_SIZE; i++) { | |
23 | + array[( i * stride_size) % len]++; | |
24 | + } | |
25 | + gettimeofday(&end, &tz); | |
26 | + | |
27 | + free(array); | |
28 | + return ((end.tv_sec * 1e6) + end.tv_usec - (start.tv_sec * 1e6) - start.tv_usec); | |
29 | +} | |
30 | + | |
31 | +int main() | |
32 | +{ | |
33 | + int i, j; | |
34 | + | |
35 | + for (j = 4; j <= 32; j*=2) { | |
36 | + printf("Stride size: %u\n", j); | |
37 | + for (i = 1024; i <= 32 * MB; i*=2) { | |
38 | + u64 access_time = access_time_1(i, j); | |
39 | + u64 overhead = 10; | |
40 | + double avg = (access_time * 1000.0)/(2 * MAX_ARRAY_SIZE); | |
41 | + printf("Size: %uKB access: %lluus, avg: %gns\n", i >> 10, access_time, avg - overhead); | |
42 | + } | |
43 | + } | |
44 | + return 0; | |
45 | +} |