-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_tree_perf.cpp
172 lines (130 loc) · 4.29 KB
/
test_tree_perf.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
170
171
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <sys/time.h>
#include "AVLTree.h"
#include "BinaryTree.h"
using namespace std;
typedef AVLTreeNode<int> AVLIntNode;
typedef AVLTree<int> AVLIntTree;
typedef BinaryTreeNode<int> BSTIntNode;
typedef BinaryTree<int> BSTIntTree;
void printHeader() {
cout << endl << "#####################################" << endl;
}
const char* const getYesNo(bool value) {
if (value) {
return "Yes";
} else {
return "No";
}
}
// Populate array with integers. If fRandom is true, the values will be
// from 0 to range. If fRandom is false, the integer will match the index in
// the array (completely sorted ascending).
void populateArray(int* array, int size, int range, bool fRandom) {
for (int index = 0; index < size; index++) {
if (fRandom) {
array[index] = rand() % range;
} else {
array[index] = index;
}
}
}
double diffTime(timeval t1, timeval t2) {
// http://stackoverflow.com/questions/2150291/how-do-i-measure-a-time-interval-in-c
double elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0; // sec to ms
elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0; // us to ms
return elapsedTime / 1000.0; // to seconds
}
void compareTrees(int numNodesAndRange, bool fRandom) {
//
// Test insertions
//
printHeader();
cout << endl << numNodesAndRange << " Insertions..." << endl;
AVLIntTree avlTree;
BSTIntTree bstTree;
int* array = new int[numNodesAndRange];
populateArray(array, numNodesAndRange, numNodesAndRange, fRandom);
timeval start;
timeval end;
// BinaryTree
gettimeofday(&start, NULL);
for (int index = 0; index < numNodesAndRange; index++) {
bstTree.insert(array[index]);
}
gettimeofday(&end, NULL);
cout << endl << "BST Elapsed: " << diffTime(start, end) << endl;
// AVLTree
gettimeofday(&start, NULL);
for (int index = 0; index < numNodesAndRange; index++) {
avlTree.insert(array[index]);
}
gettimeofday(&end, NULL);
cout << "AVL Elapsed: " << diffTime(start, end) << endl;
//
// Test searches
//
cout << endl << numNodesAndRange << " Searches..." << endl;
// BinaryTree
gettimeofday(&start, NULL);
for (int index = 0; index < numNodesAndRange; index++) {
bstTree.find(array[index]);
//assert(pNode != NULL);
}
gettimeofday(&end, NULL);
cout << endl << "BST Elapsed: " << diffTime(start, end) << endl;
// AVLTree
gettimeofday(&start, NULL);
for (int index = 0; index < numNodesAndRange; index++) {
avlTree.find(array[index]);
//assert(pNode != NULL);
}
gettimeofday(&end, NULL);
cout << "AVL Elapsed: " << diffTime(start, end) << endl;
//
// Test deletions
//
cout << endl << numNodesAndRange << " Deletions..." << endl;
// BinaryTree
gettimeofday(&start, NULL);
for (int index = 0; index < numNodesAndRange; index++) {
bstTree.remove(array[index]);
}
gettimeofday(&end, NULL);
cout << endl << "BST Elapsed: " << diffTime(start, end) << endl;
// AVLTree
gettimeofday(&start, NULL);
for (int index = 0; index < numNodesAndRange; index++) {
avlTree.remove(array[index]);
}
gettimeofday(&end, NULL);
cout << "AVL Elapsed: " << diffTime(start, end) << endl;
//
// Cleanup
//
delete [] array;
}
int main(int /*argc*/, char** /*argv*/) {
cout << endl << "Beginning Tree Perf Test." << endl;
srand(time(NULL));
printHeader();
cout << endl << "SORTED NUMBERS" << endl;
compareTrees(1000, false /*fRandom*/);
compareTrees(10000, false /*fRandom*/);
//compareTrees(100000, false /*fRandom*/);
//compareTrees(1000000, false /*fRandom*/);
//compareTrees(10000000, false /*fRandom*/);
//compareTrees(100000000, false /*fRandom*/);
printHeader();
cout << endl << "RANDOMLY GENERATED NUMBERS" << endl;
compareTrees(1000, true /*fRandom*/);
compareTrees(10000, true /*fRandom*/);
//compareTrees(100000, true /*fRandom*/);
//compareTrees(1000000, true /*fRandom*/);
//compareTrees(10000000, true /*fRandom*/);
//compareTrees(100000000, true /*fRandom*/);
printHeader();
cout << endl << "Done With Tree Perf Test." << endl;
}