-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
157 lines (137 loc) · 4.03 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
#include <future>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
#if __GNUC__ >= 10
#include <ranges>
#endif
using namespace std;
int inc(int x) {
return x + 1;
}
int fib(int n) {
switch (n) {
case 0:
case 1:
return n;
default:
return fib(n - 2) + fib(n - 1);
}
}
auto fact(int x) {
if (x == 0)
return 1;
else {
auto tmp = fact(x - 1);
return tmp % 100 * x;
}
}
template <launch policy>
auto makeLazyVec = [](auto vars) {
vector<shared_future<int>> fvars;
transform(begin(vars), end(vars), back_inserter(fvars),
[](int x) -> shared_future<int> {
return async(
policy,
[](int y) -> int { return y; },
x)
.share();
});
return fvars;
};
auto lazyMap = [](auto in, auto func) -> decltype(in) {
decltype(in) nxt;
transform(begin(in), end(in), back_inserter(nxt), func);
return nxt;
};
template <launch policy>
auto makeLazyFunc = [](auto func) {
return [func](auto in) {
return async(
policy, [func](auto in) { return func(in.get()); }, in);
};
};
auto lazyTake = [](auto vec, int num) {
decltype(vec) ret;
copy_n(begin(vec), min((int)vec.size(), num), back_inserter(ret));
return ret;
};
class stopWatch {
private:
decltype(std::chrono::high_resolution_clock::now()) start = std::chrono::high_resolution_clock::now();
public:
~stopWatch() {
const auto finish = std::chrono::high_resolution_clock::now();
const auto durationMilliSeconds = std::chrono::duration_cast<std::chrono::milliseconds>(finish - start).count();
std::cout << durationMilliSeconds << " ms" << std::endl;
}
};
template <launch policy>
class lazyVec {
private:
const vector<shared_future<int>> _fv;
public:
lazyVec(const vector<int>& v) : _fv{makeLazyVec<policy>(v)} {}
lazyVec(const vector<shared_future<int>>& v) : _fv{v} {}
decltype(_fv.cbegin()) begin() const noexcept {
return _fv.cbegin();
}
decltype(_fv.cend()) end() const noexcept {
return _fv.cend();
}
template <typename TFunc>
const lazyVec map(TFunc func) const {
return lazyVec(lazyMap(_fv, func));
}
const lazyVec take(int num) const {
return lazyTake(_fv, num);
}
long long accumulate() const {
return std::accumulate(_fv.begin(), _fv.end(), 0LL, [](auto l, auto r) { return l + r.get(); });
}
};
constexpr int VARSNUM = 40;
constexpr int TAKENUM = 35;
static_assert(VARSNUM >= TAKENUM);
int main() {
vector<int> vars;
for (auto i = 0; i < VARSNUM; i++) vars.push_back(i);
{ // deferred
stopWatch t;
vector<shared_future<int>> fvars = makeLazyVec<launch::deferred>(vars);
fvars = lazyMap(fvars, makeLazyFunc<launch::deferred>(fib));
fvars = lazyMap(fvars, makeLazyFunc<launch::deferred>(inc));
fvars = lazyMap(fvars, makeLazyFunc<launch::deferred>(inc));
fvars = lazyTake(fvars, TAKENUM);
cout << accumulate(fvars.begin(), fvars.end(), 0LL, [](auto l, auto r) { return l + r.get(); }) << endl;
}
{ // method chain, deferred
stopWatch t;
const auto v = lazyVec<launch::deferred>(vars);
const auto ans = v.map(makeLazyFunc<launch::deferred>(fib))
.map(makeLazyFunc<launch::deferred>(inc))
.map(makeLazyFunc<launch::deferred>(inc))
.take(TAKENUM)
.accumulate();
cout << ans << endl;
}
{ // async
stopWatch t;
const auto v = lazyVec<launch::async>(vars);
const auto ans = v.map(makeLazyFunc<launch::async>(fib))
.map(makeLazyFunc<launch::async>(inc))
.map(makeLazyFunc<launch::async>(inc))
.take(TAKENUM)
.accumulate();
cout << ans << endl;
}
#if __GNUC__ >= 10
{ // ranges
stopWatch t;
long long base = 0;
for (auto i : vars | views::transform(fib) | views::transform(inc) | views::transform(inc) | views::take(TAKENUM)) base += i;
cout << base << endl;
}
#endif
}