-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·169 lines (149 loc) · 4.86 KB
/
main.cpp
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
/*!
* USAGE: ./tree-benchmark [input_db] [output-file]
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "abp.h"
#include "avl.h"
#define AVL_PRECISION 50
void progressBar(int progress, int total) {
int barWidth = 69; // Largura da barra de progresso
float percent = (float)progress / (float)total;
int progressWidth = (int)(percent * barWidth);
printf("[");
for (int i = 0; i < progressWidth; i++) {
printf("=");
}
for (int i = progressWidth; i < barWidth; i++) {
printf(" ");
}
printf("] %d%%\r", (int)(percent * 100));
fflush(stdout);
}
int main(int argc, char **argv) {
// Set RNG seed
srand(time(0));
// Get size of tree
char buff[1024];
int size_of_tree = 0;
FILE * db = fopen(argv[1], "r");
while(fgets(buff, sizeof(buff), db) != NULL) {
size_of_tree++;
}
fclose(db);
// Output file
FILE * out = fopen(argv[2], "w");
fprintf(out, "size,abp_insert,avl_insert,abp_fqe,abp_mqe,abp_lqe,avl_fqe,avl_mqe,avl_lqe,abp_rand_mean,avl_rand_mean\n");
fprintf(out, "%d,", size_of_tree);
// Create tree
pABP *ABP = NULL;
pAVL *AVL = NULL;
// Insert ABP
int counter = 0;
printf("Inserting ABP...\n");
db = fopen(argv[1], "r");
clock_t start = clock();
int tmp;
while(fgets(buff, sizeof(buff), db) != NULL) {
tmp = atoi(buff);
ABP = InsereArvore(ABP, tmp);
progressBar(counter, size_of_tree);
counter++;
}
clock_t end = clock();
fclose(db);
double insert_time_ABP = (double) (end - start) / CLOCKS_PER_SEC;
printf("[ABP] Insert %d integers: %lf\n", size_of_tree, insert_time_ABP);
fprintf(out, "%.10f,", insert_time_ABP);
// Insert AVL
counter = 0;
printf("Inserting AVL...\n");
db = fopen(argv[1], "r");
start = clock();
while(fgets(buff, sizeof(buff), db) != NULL) {
tmp = atoi(buff);
AVL = InsereAVL(AVL, tmp);
progressBar(counter, size_of_tree);
counter++;
}
end = clock();
fclose(db);
double insert_time_AVL = (double) (end - start) / CLOCKS_PER_SEC;
printf("[AVL] Insert %d integers: %lf\n", size_of_tree, insert_time_AVL);
fprintf(out, "%.10f,", insert_time_AVL);
printf("Query extremities...\n");
// Query First, Middle, Last
// ABP
int first = 1;
int middle = size_of_tree/2;
start = clock();
consultaABP(ABP, first);
end = clock();
double search_time = (double) (end - start) / CLOCKS_PER_SEC;
printf("[ABP] Query First: %lf\n", search_time);
fprintf(out, "%.10f,", search_time);
start = clock();
consultaABP(ABP, middle);
end = clock();
search_time = (double) (end - start) / CLOCKS_PER_SEC;
printf("[ABP] Query Middle: %lf\n", search_time);
fprintf(out, "%.10f,", search_time);
start = clock();
consultaABP(ABP, size_of_tree);
end = clock();
search_time = (double) (end - start) / CLOCKS_PER_SEC;
printf("[ABP] Query Last: %lf\n", search_time);
fprintf(out, "%.10f,", search_time);
// AVL
start = clock();
consultaAVL(AVL, first);
end = clock();
search_time = (double) (end - start) / CLOCKS_PER_SEC;
printf("[AVL] Query First: %lf\n", search_time);
fprintf(out, "%.10f,", search_time);
start = clock();
consultaAVL(AVL, middle);
end = clock();
search_time = (double) (end - start) / CLOCKS_PER_SEC;
printf("[AVL] Query Middle: %lf\n", search_time);
fprintf(out, "%.10f,", search_time);
start = clock();
consultaAVL(AVL, size_of_tree);
end = clock();
search_time = (double) (end - start) / CLOCKS_PER_SEC;
printf("[AVL] Query Last: %lf\n", search_time);
fprintf(out, "%.10f,", search_time);
printf("Querying random...\n");
// Query Random
// ABP
double sum = 0;
for (int i = 0; i < AVL_PRECISION; i++) {
tmp = (rand() % size_of_tree) + 1;
start = clock();
consultaABP(ABP, tmp);
end = clock();
search_time = (double) (end - start) / CLOCKS_PER_SEC;
sum += search_time;
// printf("[ABP] Query %d: %lf\n", tmp, search_time);
}
printf("[ABP] %d Mean %.10lf\n", AVL_PRECISION, (sum / AVL_PRECISION));
fprintf(out, "%.10f,", (sum / AVL_PRECISION));
// AVL
sum = 0;
for (int i = 0; i < AVL_PRECISION; i++) {
tmp = (rand() % size_of_tree) + 1;
start = clock();
consultaAVL(AVL, tmp);
end = clock();
search_time = (double) (end - start) / CLOCKS_PER_SEC;
sum += search_time;
// printf("[AVL] Query %d: %lf\n", tmp, search_time);
}
printf("[AVL] %d Mean %.10lf\n", AVL_PRECISION, (sum / AVL_PRECISION));
fprintf(out, "%.10f", (sum / AVL_PRECISION));
// Close output file
fclose(out);
return 0;
}