-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
98 lines (74 loc) · 2.58 KB
/
main.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "bbmalloc.h"
#include <time.h>
#include <stdlib.h>
#define NUM_DATA_BLOCKS 512
#define BUFFER_SIZE 1000000
#define VOLATILE_DATA_BLOCKS 100
void randomIdxVector(int* vec, int size, int randomMax){
for (int i = 0; i < size; ++i) vec[i] = -1;
for (int i = 0; i < size; ++i) {
int foundValidIdx = 0;
int r;
while (!foundValidIdx) {
r = rand()%randomMax;
for (int j = 0; j < size; ++j) {
if (vec[j] == r) {
r = -1;
break;
}
}
if (r != -1) {
vec[i] = r;
foundValidIdx = 1;
}
}
}
}
int main(int argc, char* argv[]) {
int randomIdxs[NUM_DATA_BLOCKS];
randomIdxVector(randomIdxs, NUM_DATA_BLOCKS, NUM_DATA_BLOCKS);
int randomVol[VOLATILE_DATA_BLOCKS];
int randomValues[VOLATILE_DATA_BLOCKS];
randomIdxVector(randomVol, VOLATILE_DATA_BLOCKS, NUM_DATA_BLOCKS);
randomIdxVector(randomValues, VOLATILE_DATA_BLOCKS, NUM_DATA_BLOCKS);
int *dataPointers[NUM_DATA_BLOCKS];
clock_t t;
t = clock();
for (int i = 0; i < NUM_DATA_BLOCKS; ++i) {
dataPointers[i] = bbmalloc(i*BUFFER_SIZE*sizeof(int));
dataPointers[i][0] = i;
}
for (int i = 0; i < VOLATILE_DATA_BLOCKS; ++i) {
bbfree(dataPointers[randomVol[i]]);
}
for (int i = 0; i < VOLATILE_DATA_BLOCKS; ++i) {
dataPointers[randomVol[i]] = bbmalloc(randomValues[i]*BUFFER_SIZE*sizeof(int));
dataPointers[randomVol[i]][0] = randomValues[i];
}
for (int i = 0; i < NUM_DATA_BLOCKS; ++i) {
bbfree(dataPointers[randomIdxs[i]]);
}
t = clock() - t;
double timeTaken = 1000*((double)t)/CLOCKS_PER_SEC;
printf("bbmalloc time taken: %lf ms\n", timeTaken);
int *dataPointersMalloc[NUM_DATA_BLOCKS];
t = clock();
for (int i = 0; i < NUM_DATA_BLOCKS; ++i) {
dataPointersMalloc[i] = malloc(i*BUFFER_SIZE*sizeof(int));
dataPointersMalloc[i][0] = i;
}
for (int i = 0; i < VOLATILE_DATA_BLOCKS; ++i) {
free(dataPointersMalloc[randomVol[i]]);
}
for (int i = 0; i < VOLATILE_DATA_BLOCKS; ++i) {
dataPointersMalloc[randomVol[i]] = malloc(randomValues[i]*BUFFER_SIZE*sizeof(int));
dataPointersMalloc[randomVol[i]][0] = randomValues[i];
}
for (int i = 0; i < NUM_DATA_BLOCKS; ++i) {
free(dataPointersMalloc[randomIdxs[i]]);
}
t = clock() - t;
timeTaken = 1000*((double)t)/CLOCKS_PER_SEC;
printf("libc malloc time taken: %lf ms\n", timeTaken);
return 0;
}