-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.c
355 lines (265 loc) · 9.77 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/* Copyright (c) 2011 Terence Siganakis.
This file is part of TnyDB.
TnyDB is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
TnyDB is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with TnyDB. If not, see <http://www.gnu.org/licenses/>.
*/
/* Contact: Terence Siganakis <terence@siganakis.com> */
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include "tnyDB_tmap.h"
#include "tnyDB_wtree.h"
#include "tnyDB_tarray.h"
#include "tnyDB_tword.h"
void tnyDB_tmap_access_test(int * values, int length) {
tnyDB_mem_init();
double elapsed; // in milliseconds
clock_t start, end;
start = clock();
printf("Building TMAP... ");
tnyDB_tmap *tree = tnyDB_tmap_create(values, length);
end = clock();
elapsed = ((double) (end - start) * 1000) / CLOCKS_PER_SEC;
printf("Done (took %fms)\n", elapsed);
tnyDB_mem_statistics memStats = tnyDB_mem_get_statistics();
// printf("\n====== TMAP Mem Stats ======\n");
printf("Bytes current: %i\n", memStats.bytes_current);
// printf("Bytes total: %i\n", memStats.bytes_total);
// printf("Allocations: %i\n", memStats.allocations);
// printf("Frees: %i\n", memStats.frees);
// printf("====== TMAP Mem Stats ======\n\n");
printf("Testing TMAP access performance...");
start = clock();
for (int i = 0; i < length; i++) { //
if (values[i] != tnyDB_tmap_access(tree, i)) {
end = clock();
elapsed = ((double) (end - start) * 1000) / CLOCKS_PER_SEC;
fprintf(stderr, "\nmissmatch at: %i, found %i, expected %i (%f)\n", i, tnyDB_tmap_access(tree, i),
values[i], elapsed);
exit(-1);
}
}
end = clock();
elapsed = ((double) (end - start) * 1000) / CLOCKS_PER_SEC;
printf("TMAP Passed test in %fms\n\n", elapsed);
}
void tnyDB_wtree_access_test(int * values, int length) {
tnyDB_mem_init();
double elapsed; // in milliseconds
clock_t start, end;
start = clock();
printf("Building WTREE... ");
tnyDB_wtree *tree = tnyDB_wtree_create(values, length);
end = clock();
elapsed = ((double) (end - start) * 1000) / CLOCKS_PER_SEC;
printf("Done (took %fms)\n", elapsed);
tnyDB_mem_statistics memStats = tnyDB_mem_get_statistics();
// printf("\n====== WTREE Mem Stats ======\n");
printf("Bytes current: %i\n", memStats.bytes_current);
// printf("Bytes total: %i\n", memStats.bytes_total);
// printf("Allocations: %i\n", memStats.allocations);
// printf("Frees: %i\n", memStats.frees);
// printf("====== WTREE Mem Stats ======\n\n");
printf("Testing wTree access performance...");
start = clock();
for (int i = 0; i < length; i++) { //
if (values[i] != tnyDB_wtree_access(tree, i)) {
end = clock();
elapsed = ((double) (end - start) * 1000) / CLOCKS_PER_SEC;
fprintf(stderr, "\nmissmatch at: %i, found %i, expected %i (%f)\n", i, tnyDB_wtree_access(tree, i),
values[i], elapsed);
exit(-1);
}
}
end = clock();
elapsed = ((double) (end - start) * 1000) / CLOCKS_PER_SEC;
printf("wtree Passed test in %fms\n\n", elapsed);
}
void tnyDB_tarray_access_test(int * values, int length) {
tnyDB_mem_init();
double elapsed; // in milliseconds
clock_t start, end;
start = clock();
printf("Building ARRAY... ");
tnyDB_tarray *tree = tnyDB_tarray_create(values, length);
end = clock();
elapsed = ((double) (end - start) * 1000) / CLOCKS_PER_SEC;
printf("Done (took %fms)\n", elapsed);
tnyDB_mem_statistics memStats = tnyDB_mem_get_statistics();
// printf("\n====== array Mem Stats ======\n");
printf("Bytes current: %i\n", memStats.bytes_current);
// printf("Bytes total: %i\n", memStats.bytes_total);
// printf("Allocations: %i\n", memStats.allocations);
// printf("Frees: %i\n", memStats.frees);
// printf("====== array Mem Stats ======\n\n");
printf("Testing ARRAY access performance...");
start = clock();
for (int i = 0; i < length; i++) { //
if (values[i] != tnyDB_tarray_access(tree, i)) {
end = clock();
elapsed = ((double) (end - start) * 1000) / CLOCKS_PER_SEC;
fprintf(stderr, "\nmissmatch at: %i, found %i, expected %i (%f)\n", i, tnyDB_tarray_access(tree, i),
values[i], elapsed);
exit(-1);
}
}
end = clock();
elapsed = ((double) (end - start) * 1000) / CLOCKS_PER_SEC;
printf("ARRAY Passed test in %fms\n\n", elapsed);
}
int tnyDB_access_test(int keys, int length) {
srand(time(NULL));
int *values = malloc(length * sizeof(int));
printf("Generating data...");
for (int i = 0; i < length; i++) {
values[i] = rand() % keys;
}
printf("Done\nh");
tnyDB_tarray_access_test(values, length);
tnyDB_tmap_access_test(values, length);
tnyDB_wtree_access_test(values, length);
free(values);
return 1;
}
void tnyDB_tarray_seek_test(int * values, int length, int seeks) {
tnyDB_mem_init();
double elapsed; // in milliseconds
clock_t start, end;
start = clock();
printf("Building ARRAY... ");
tnyDB_tarray *tree = tnyDB_tarray_create(values, length);
end = clock();
elapsed = ((double) (end - start) * 1000) / CLOCKS_PER_SEC;
printf("Done (took %fms)\n", elapsed);
tnyDB_mem_statistics memStats = tnyDB_mem_get_statistics();
// printf("\n====== ARRAY Mem Stats ======\n");
printf("Bytes current: %i\n", memStats.bytes_current);
// printf("Bytes total: %i\n", memStats.bytes_total);
// printf("Allocations: %i\n", memStats.allocations);
// printf("Frees: %i\n", memStats.frees);
printf("Testing ARRAY SEEK performance... ");
start = clock();
for (int i = 0; i < seeks; i++) { //
int seeking = values[i];
TWORD * results = tnyDB_tarray_seek(tree, seeking);
// Now in the results, a bit at "i" should be set
if (tnyDB_tword_bit_is_set(results, i) == 0) {
end = clock();
elapsed = ((double) (end - start) * 1000) / CLOCKS_PER_SEC;
fprintf(stderr, "\n ARRAY SEEK: missmatch at: %i, BIT NOT SET\n", i);
exit(-1);
}
}
end = clock();
elapsed = ((double) (end - start) * 1000) / CLOCKS_PER_SEC;
printf("Passed test in %fms\n\n", elapsed);
}
void tnyDB_tmap_seek_test(int * values, int length, int seeks) {
tnyDB_mem_init();
double elapsed; // in milliseconds
clock_t start, end;
start = clock();
printf("Building TMAP... ");
tnyDB_tmap *tree = tnyDB_tmap_create(values, length);
end = clock();
elapsed = ((double) (end - start) * 1000) / CLOCKS_PER_SEC;
printf("Done (took %fms)\n", elapsed);
tnyDB_mem_statistics memStats = tnyDB_mem_get_statistics();
// printf("\n====== TMAP Mem Stats ======\n");
printf("Bytes current: %i\n", memStats.bytes_current);
// printf("Bytes total: %i\n", memStats.bytes_total);
// printf("Allocations: %i\n", memStats.allocations);
// printf("Frees: %i\n", memStats.frees);
// printf("====== TMAP Mem Stats ======\n\n");
printf("Testing TMAP SEEK performance... ");
start = clock();
for (int i = 0; i < seeks; i++) { //
int seeking = values[i];
TWORD * results = tnyDB_tmap_seek(tree, seeking);
// Now in the results, a bit at "i" should be set
if (tnyDB_tword_bit_is_set(results, i) == 0) {
end = clock();
elapsed = ((double) (end - start) * 1000) / CLOCKS_PER_SEC;
fprintf(stderr, "\n TMAP Seek: missmatch at: %i, BIT NOT SET\n", i);
exit(-1);
}
}
end = clock();
elapsed = ((double) (end - start) * 1000) / CLOCKS_PER_SEC;
printf(" Passed test in %fms\n\n", elapsed);
}
void tnyDB_wtree_seek_test(int * values, int length, int seeks) {
tnyDB_mem_init();
double elapsed; // in milliseconds
clock_t start, end;
start = clock();
printf("Building WTREE... ");
tnyDB_wtree *tree = tnyDB_wtree_create(values, length);
end = clock();
elapsed = ((double) (end - start) * 1000) / CLOCKS_PER_SEC;
printf("Done (took %fms)\n", elapsed);
tnyDB_mem_statistics memStats = tnyDB_mem_get_statistics();
// printf("\n====== TMAP Mem Stats ======\n");
printf("Bytes current: %i\n", memStats.bytes_current);
// printf("Bytes total: %i\n", memStats.bytes_total);
// printf("Allocations: %i\n", memStats.allocations);
// printf("Frees: %i\n", memStats.frees);
// printf("====== TMAP Mem Stats ======\n\n");
printf("Testing WTREE SEEK performance... ");
start = clock();
for (int i = 0; i < seeks; i++) { //
int seeking = values[i];
TWORD * results = tnyDB_wtree_seek(tree, seeking);
// Now in the results, a bit at "i" should be set
if (tnyDB_tword_bit_is_set(results, i) == 0) {
end = clock();
elapsed = ((double) (end - start) * 1000) / CLOCKS_PER_SEC;
fprintf(stderr, "\n WTREE Seek: missmatch at: %i, BIT NOT SET\n", i);
exit(-1);
}
}
end = clock();
elapsed = ((double) (end - start) * 1000) / CLOCKS_PER_SEC;
printf(" Passed test in %fms\n\n", elapsed);
}
int tnyDB_seek_test(int keys, int length, int seeks) {
srand(time(NULL));
int *values = malloc(length * sizeof(int));
printf("Generating data...");
for (int i = 0; i < length; i++) {
values[i] = rand() % keys;
}
printf("Done\n\n");
tnyDB_tarray_seek_test(values, length, seeks);
tnyDB_tmap_seek_test(values, length, seeks);
tnyDB_wtree_seek_test(values, length, seeks);
free(values);
return 1;
}
int main(int argc, char *argv[]) {
if (argc < 4 || argc > 4){
printf("Please supply 3 arguments:\n");
printf("\t1.\t The number of values to generate\n");
printf("\t2.\t The number of distinct values\n");
printf("\t3.\t The number of times the structures should be seeked\n");
printf("You supplied %i arguments\n", argc);
return EXIT_SUCCESS;
}
int values= atoi(argv[1]);
int keys = atoi(argv[2]);
int seeks = atoi(argv[3]);
tnyDB_access_test(keys, values);
tnyDB_seek_test(keys, values, seeks);
// int values[10] = {1, 4, 7, 4, 5, 6, 7, 9, 1, 2};
// tnyDB_tmap_seek_test(values, 10);
return EXIT_SUCCESS;
}