-
Notifications
You must be signed in to change notification settings - Fork 3
/
example-transform.main.cpp
60 lines (51 loc) · 1.74 KB
/
example-transform.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
#include <ProfilerLib/Profiler.hpp> // for Profiler
#include <ProfilerLib/ScopeEvent.hpp> // for ScopeEvent
#include <algorithm> // for transform
#include <execution>
#include <numeric> // for iota
#include <vector> // for vector, allocator
/**
* This example demonstrates using the profiler to compare different execution policies
* for std::transform where the duration of each calculation is dependent on the input.
*/
int main() {
constexpr auto size = 1000;
Profiler p("profiler", "trace-transform.json");
p.setProcessName("Example: std::transform");
p.setThreadName("main thread");
ScopeEvent mainScope(p, "int main()");
auto calculate = [&p](auto v) {
ScopeEvent e(p, "calculate");
decltype(v) res = 1;
for (int i = 0; i < 1000 * v; i++) {
res *= v;
}
return res;
};
std::vector<double> v(size);
{
ScopeEvent i(p, "initializing");
std::iota(v.begin(), v.end(), 0);
}
std::vector<double> res(size);
{
ScopeEvent t(p, "std::transform");
std::transform(v.begin(), v.end(), res.begin(), calculate);
}
{
ScopeEvent t(p, "std::transform seq");
std::transform(std::execution::seq, v.begin(), v.end(), res.begin(), calculate);
}
{
ScopeEvent t(p, "std::transform unseq");
std::transform(std::execution::unseq, v.begin(), v.end(), res.begin(), calculate);
}
{
ScopeEvent t(p, "std::transform par");
std::transform(std::execution::par, v.begin(), v.end(), res.begin(), calculate);
}
{
ScopeEvent t(p, "std::transform par_unseq");
std::transform(std::execution::par_unseq, v.begin(), v.end(), res.begin(), calculate);
}
}