-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSTL_ParallelAlgorithms.cpp
More file actions
101 lines (85 loc) · 2.88 KB
/
STL_ParallelAlgorithms.cpp
File metadata and controls
101 lines (85 loc) · 2.88 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
// ===========================================================================
// STL and Parallel Algorithms
// ===========================================================================
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <chrono>
#include <random>
#include <vector>
#include <execution>
namespace STL_Parallel_Algorithms
{
constexpr size_t TestSize{ 1'000'000 };
constexpr size_t IterationCount{ 4 };
void printResults (
std::string tag,
std::chrono::high_resolution_clock::time_point startTime,
std::chrono::high_resolution_clock::time_point endTime)
{
std::cout
<< std::setw(10)
<< std::left
<< tag
<< std::fixed
<< std::setprecision(6)
<< std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(endTime - startTime).count()
<< " msecs."
<< std::endl;
}
template <typename T>
void fillTestVector(std::vector<T>& numbers)
{
std::random_device device;
for (auto& number : numbers) {
number = static_cast<T>(device());
}
}
template <typename T>
void testSeq(const std::vector<T>& numbers)
{
for (size_t i{}; i != IterationCount; ++i)
{
std::vector<T> copyToSort{ numbers };
const auto startTime{ std::chrono::high_resolution_clock::now() };
std::sort(
copyToSort.begin(),
copyToSort.end()
);
const auto endTime{ std::chrono::high_resolution_clock::now() };
printResults("Serial", startTime, endTime);
}
}
template <typename T>
void testPar(const std::vector<T>& numbers)
{
for (size_t i{}; i != IterationCount; ++i)
{
std::vector<T> copyToSort{ numbers };
const auto startTime{ std::chrono::high_resolution_clock::now() };
// same sort call as above, but with 'par_unseq' or 'par':
std::sort(
std::execution::par,
copyToSort.begin(),
copyToSort.end()
);
const auto endTime{ std::chrono::high_resolution_clock::now() };
printResults("Parallel", startTime, endTime);
}
}
}
void test_STL_Parallel_Algorithms()
{
using namespace STL_Parallel_Algorithms;
std::cout
<< "Testing with " << TestSize << " doubles ..."
<< std::endl << std::endl;
std::vector<double> numbers(TestSize); // vector of length TestSize
fillTestVector(numbers);
testSeq(numbers);
std::cout << std::endl;
testPar(numbers);
}
// ===========================================================================
// End-of-File
// ===========================================================================