-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.cpp
More file actions
70 lines (59 loc) · 2.2 KB
/
Copy pathbenchmark.cpp
File metadata and controls
70 lines (59 loc) · 2.2 KB
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
#include "bst.hpp"
#include <iostream>
#include <map>
#include <chrono> // to measure time
#include <algorithm> // for random_shuffle
#include <fstream> // to write data on file
int main(){
// open file where to put data
std::ofstream outFile;
outFile.open("data_benchmark.txt");
// create vector of unique random numbers from 0 to N_max-1
int N_max {10000};
std::vector<int> numbers;
for(int i{0}; i<N_max; ++i)
numbers.push_back(i);
std::random_shuffle(numbers.begin(), numbers.end());
// initialize trees
bst<int,char> mybst; // bst
bst<int,char> mybalbst; // balanced bst
std::map<int,char> mymap; // std::map
// start benchmark
int P{0};
for(int N{100}; N<N_max; N+=10) {
//insert nodes in the trees
for(int i{P}; i<N; ++i) {
mymap.insert({numbers[i],'a'});
mybst.insert({numbers[i],'a'});
mybalbst.insert({numbers[i],'a'});
}
P=N; // don't insert the already inserted nodes
//measuring time to find keys with std::map
auto t0_map = std::chrono::high_resolution_clock::now();
for(int k{0}; k<N; ++k){
mymap.find(numbers[k]);
}
auto t1_map = std::chrono::high_resolution_clock::now();
auto elapsed_map = std::chrono::duration_cast<std::chrono::microseconds>(t1_map-t0_map);
//measuring time to find keys with bst
auto t0_bst = std::chrono::high_resolution_clock::now();
for(int k{0}; k<N; ++k){
mybst.find(numbers[k]);
}
auto t1_bst = std::chrono::high_resolution_clock::now();
auto elapsed_bst = std::chrono::duration_cast<std::chrono::microseconds>(t1_bst-t0_bst);
// balance the tree
mybalbst.balance();
//measuring time to find keys with balanced bst
auto t0_bst_bal = std::chrono::high_resolution_clock::now();
for(int k{0}; k<N; ++k){
mybalbst.find(numbers[k]);
}
auto t1_bst_bal = std::chrono::high_resolution_clock::now();
auto elapsed_bst_bal = std::chrono::duration_cast<std::chrono::microseconds>(t1_bst_bal-t0_bst_bal);
// write results to file
outFile << N << "\t" << (double(elapsed_map.count()))/((double)N) << "\t" << (double(elapsed_bst.count()))/((double)N) << "\t" << (double(elapsed_bst_bal.count()))/((double)N) << "\n";
}
outFile.close();
return 0;
}