-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
202 lines (171 loc) · 5.61 KB
/
main.cpp
File metadata and controls
202 lines (171 loc) · 5.61 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
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
#include <iostream>
#include "datastructures/Queue.h"
#include "parallel/ThreadPoolExecutor.h"
#include "datastructures/FibonacciHeap.h"
#include "datastructures/Heap.h"
#include <array>
#include <queue>
#include <ctime>
#include <cstdlib>
#include <chrono>
#include <vector>
#include <boost/heap/fibonacci_heap.hpp>
using namespace std::chrono;
using data_type = int;
void testStdQueue(std::vector<int>& arr, data_type& data, int push_threshold) {
std::queue<data_type> std_q;
for(int i = 0; i< arr.size(); i++) {
int r = arr[i];
if(r < push_threshold) {
std_q.push(data);
} else {
size_t size = std_q.size();
if( size > 0) {
data_type a = std_q.front();
std_q.pop();
}
}
}
}
void testMyQueue(std::vector<int>& arr, data_type& data, int push_threshold) {
Queue<data_type,20> q;
for(int i = 0; i< arr.size(); i++) {
int r = arr[i];
if(r < push_threshold) {
q.push(data);
} else {
size_t size = q.size();
if( size > 0) {
data_type a = q.front();
q.pop();
}
}
}
}
void testMyFibonacci(std::vector<int>& arr, data_type& data, int push_threshold) {
FibonacciHeap<int> q;
for(int i = 0; i< arr.size(); i++) {
int r = arr[i];
if(r < push_threshold) {
q.push(r);
} else {
size_t size = q.size();
if( size > 0) {
data_type a = q.top();
q.pop();
}
}
}
}
void testStdPrioQueu(std::vector<int>& arr, data_type& data, int push_threshold) {
std::priority_queue<int, std::vector<int>, std::greater<int>> q;
for(int i = 0; i< arr.size(); i++) {
int r = arr[i];
if(r < push_threshold) {
q.push(r);
} else {
size_t size = q.size();
if( size > 0) {
data_type a = q.top();
q.pop();
}
}
}
}
void testMyPrioQueu(std::vector<int>& arr, data_type& data, int push_threshold) {
Heap<int> q;
for(int i = 0; i< arr.size(); i++) {
int r = arr[i];
if(r < push_threshold) {
q.push(r);
} else {
size_t size = q.size();
if( size > 0) {
data_type a = q.top();
q.pop();
}
}
}
}
void testBoostFibonacciHeap(std::vector<int>& arr, data_type& data, int push_threshold) {
boost::heap::fibonacci_heap<int> q;
for(int i = 0; i< arr.size(); i++) {
int r = arr[i];
if(r < push_threshold) {
q.push(r);
} else {
size_t size = q.size();
if( size > 0) {
data_type a = q.top();
q.pop();
}
}
}
}
void ValidateFibonacciHeap(std::vector<int>& arr, data_type& data, int push_threshold) {
std::priority_queue<int, std::vector<int>, std::greater<int>> q;
Heap<int> q2;
for(int i = 0; i< arr.size(); i++) {
int r = arr[i];
if(r < push_threshold) {
q.push(r);
q2.push(r);
} else {
size_t size = q.size();
if( size > 0) {
data_type a = q.top();
if(a != q2.top()) {
std::cout << "WRONG" << std::endl;
}
q.pop();
q2.pop();
}
}
}
std::cout << "Validated" << std::endl;
}
int timeIt(std::function<void()> f){
milliseconds start = duration_cast< milliseconds >(
system_clock::now().time_since_epoch()
);
f();
milliseconds end = duration_cast< milliseconds >(
system_clock::now().time_since_epoch()
);
return (end - start).count();
}
void threadPoolQueusTest() {
int N = 5000000;
// Should be between 1 and 100;
int push_threshold = 80;
data_type data;
std::vector<int> arr;
std::srand(std::time(nullptr));
for(int i = 0; i< N; i++) {
arr.push_back(std::rand() % 100);
}
std::function<void()> test_std_queue = std::bind(testStdQueue, arr, data, push_threshold);
std::function<void()> test_my_queue = std::bind(testMyQueue, arr, data, push_threshold);
std::function<void()> test_std_prioqueue = std::bind(testStdPrioQueu, arr, data, push_threshold);
std::function<void()> test_my_prioqueue = std::bind(testStdPrioQueu, arr, data, push_threshold);
std::function<void()> test_my_fibonacciHeap = std::bind(testMyFibonacci, arr, data, push_threshold);
std::function<void()> test_boost_fibonacciHeap = std::bind(testBoostFibonacciHeap, arr, data, push_threshold);
ThreadPoolExecutor<int> tpe(1);
//std::future<int> f1 = tpe.execute(std::bind(timeIt, test_std_queue));
//std::future<int> f2 = tpe.execute(std::bind(timeIt, test_my_queue));
std::future<int> f3 = tpe.execute(std::bind(timeIt, test_std_prioqueue));
std::future<int> f4 = tpe.execute(std::bind(timeIt, test_my_prioqueue));
std::future<int> f5 = tpe.execute(std::bind(timeIt, test_my_fibonacciHeap));
std::future<int> f6 = tpe.execute(std::bind(timeIt, test_boost_fibonacciHeap));
tpe.join();
//std::cout << "STD QUEUE: " << f1.get() << std::endl;
//std::cout << "MY QUEUE: " << f2.get() << std::endl;
std::cout << "STD PRIO QUEUE: " << f3.get() << std::endl;
std::cout << "MY HEAP: " << f4.get() << std::endl;
std::cout << "FIBONACCI HEAP: " << f5.get() << std::endl;
std::cout << "BOOST FIBONACCI HEAP: " << f6.get() << std::endl;
}
int main() {
threadPoolQueusTest();
return 0;
}