-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
69 lines (58 loc) · 1.98 KB
/
main.cpp
File metadata and controls
69 lines (58 loc) · 1.98 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
#include "parallel_algorithms.h"
#include "timer.h"
#include "random_init.h"
#include <cstdint>
#include <execution>
#include <algorithm>
void do_not_optimize(auto x) {
[[maybe_unused]] volatile auto y = x;
}
void test_sort() {
constexpr auto size = 10'000'000;
auto vec = random_container<std::vector<std::int32_t>>(size);
auto vec_copy = vec;
auto init = [&vec, &vec_copy]() {
vec_copy = vec; // we reinitialize
};
auto sort_seq = [&vec_copy]() {
std::sort(std::execution::seq, vec_copy.begin(), vec_copy.end());
do_not_optimize(vec_copy.begin());
};
auto sort_par = [&vec_copy]() {
std::sort(std::execution::par, vec_copy.begin(), vec_copy.end());
do_not_optimize(vec_copy.begin());
};
auto sort_par_unseq = [&vec_copy]() {
std::sort(std::execution::par_unseq, vec_copy.begin(), vec_copy.end());
do_not_optimize(vec_copy.begin());
};
auto sort_unseq = [&vec_copy]() {
std::sort(std::execution::unseq, vec_copy.begin(), vec_copy.end());
do_not_optimize(vec_copy.begin());
};
timer::time_print(init, sort_seq, 5, "sequential sort");
timer::time_print(init, sort_par, 5, "parallel sort");
timer::time_print(init, sort_par_unseq, 5, "parallel unseq sort");
timer::time_print(init, sort_unseq, 5, "unseq sort");
}
void test_find() {
constexpr auto size = 500'000'000;
auto vec = random_container<std::vector<std::int32_t>>(size);
auto last = vec.back();
auto find_seq = [last, &vec]() {
do_not_optimize(our_find_if(vec.begin(), vec.end(), [last](auto elem) {
return elem == last;
}));
};
auto find_par = [last, &vec]() {
do_not_optimize(parallel_find_if(vec.begin(), vec.end(), [last](auto elem) {
return elem == last;
}));
};
timer::time_print(find_seq, 10, "Sequential find");
timer::time_print(find_par, 10, "Parallel find");
}
int main() {
test_find();
test_sort();
}