-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
121 lines (103 loc) · 3 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
#include <iostream>
#include <data_structures/data_structures.h>
#include <fstream>
#include <set>
#include <chrono>
#include <sstream>
#include <string>
#include <vector>
#include <array>
#include <future>
#include <limits>
#include <numeric>
#include <execution>
#include <random>
#include <algorithm>
using namespace std;
struct Stopwatch
{
Stopwatch(std::chrono::nanoseconds &result) : result{result}, start{chrono::high_resolution_clock::now()} {}
~Stopwatch()
{
result = chrono::high_resolution_clock::now() - start;
}
private:
chrono::nanoseconds &result;
const chrono::time_point<chrono::high_resolution_clock> start;
};
template <typename T>
vector<T> factorize(T x)
{
vector<T> result{1};
for (T candidate{2}; candidate <= x; candidate++)
{
if (x % candidate == 0)
{
result.push_back(candidate);
x /= candidate;
candidate = 1;
}
}
return result;
}
string factor_task(unsigned long long x){
chrono::nanoseconds elapsed_ns;
vector<unsigned long long> factors;
{
Stopwatch stopwatch{ elapsed_ns};
factors = factorize(x);
}
const auto elapsed_ms = chrono::duration_cast<chrono::milliseconds>(elapsed_ns).count();
stringstream ss;
ss << elapsed_ms << "ms: Factoring " << x << " ( ";
for(auto factor : factors)
ss << factor << " ";
ss << ")\n";
return ss.str();
}
array<unsigned long long, 7> numbers{9'699'690, 179'426'549, 1'000'000'007, 4'294'967'291, 4'294'967'296, 1'307'674'368'000, 4'000};
void funcOne(){
chrono::nanoseconds elapsed_ns;
{
Stopwatch stopwatch{elapsed_ns};
vector<future<string>> factor_tasks;
for (auto number : numbers)
factor_tasks.emplace_back(async(launch::async, factor_task, number));
for (auto &task : factor_tasks)
cout << task.get();
}
const auto elapsed_ms = chrono::duration_cast<chrono::milliseconds>(elapsed_ns).count();
cout << elapsed_ms << "ms : total program time\n";
}
vector<long> make_random_vector(){
vector<long> numbers(1'000'000'000);
iota(numbers.begin(), numbers.end(), 0);
mt19937_64 urng{121216};
shuffle(numbers.begin(), numbers.end(), urng);
return numbers;
}
void funcTwo(){
cout << "Constructing random vectors...";
auto numbers_a = make_random_vector();
auto numbers_b{ numbers_a };
chrono::nanoseconds time_to_sort;
cout << " " << numbers_a.size() << " elements.\n";
cout << "Sorting with execution::seq...";
{
Stopwatch stopwatch{ time_to_sort };
sort(execution::seq, numbers_a.begin(), numbers_a.end());
}
cout << " took " << time_to_sort.count() / 1.0E9 << " sec.\n";
cout << "Sorting with execution::par...";
{
Stopwatch stopwatch{ time_to_sort };
sort(execution::par, numbers_b.begin(), numbers_b.end());
}
cout << " took " << time_to_sort.count() / 1.0E9 << " sec.\n";
}
int main()
{
// funcOne();
funcTwo();
return 0;
}