C++ Library Extensions 2022.12.09
To help learn modern C++ programming
001-parallel_sort.cpp
Go to the documentation of this file.
2#include <tpf_output.hpp>
3
4
5
7auto nl = "\n";
8auto nL = "\n\n";
10
12{
13 // STEP 1. define namespace alias
14 // and some types we will be using
15
16 namespace crd = tpf::chrono_random;
17
18 using element_t = int;
19 using container_t = std::vector<element_t>;
20
21 // STEP 2. create a random number generator
22 // we create a random number generator
23 // that generates type element_t (or double)
24 // range of generated doubles are from 1 to 100
25 auto generator = crd::random_generator<element_t>(1, 20); // from 1 to 20 inclusive
26
27 // STEP 3. create an object of type container_t, std::vector<double>
28 container_t v;
29
30 // STEP 4. fill the container with random numbers
31 // using the random number generator created at STEP 2.
32 // we generate 20 elements of type element_t, or double
33 crd::random_fill(v, generator, 10); // 10 elements of type element_t
34
35 stream <<"Random numbers : " << v << nl;
36
37 // if you don't end stream with flush here
38 // you cannot see your result. The reason is for optimization
39 stream << v.size()
40 << " random numbers are generated" << flush;
41}
42
43template<typename ContainerType, typename StopwatchType>
44void print_constainer(size_t index,
45 const ContainerType& container,
46 const StopwatchType& sw)
47 {
48 stream << index <<"- Minimum: " << container.front()
49 << ", Maximum: " << container.back()
50 << ", Elapsed: " << sw.elapsed_time() << nl;
51 }
52
54{
55 // STEP 1. define namespace alias
56 // and some types we will be using
57
58 namespace crd = tpf::chrono_random;
59
60 using element_t = double;
61 using container_t = std::vector<element_t>;
62
63 // STEP 2. create a random number generator
64 // we create a random number generator
65 // that generates type element_t (or double)
66 // range of generated doubles are from 1 to 100
67 auto generator = crd::random_generator<element_t>(1, 100); // from 1 to 100 inclusive
68
69 // STEP 3. create an object of type container_t, std::vector<double>
70 container_t v;
71
72 size_t size = 1'000'000;
73 size_t test_count = 5;
74
75 // STEP 4. fill the container with random numbers
76 // using the random number generator created at STEP 2.
77 // we generate 20 elements of type element_t, or double
78 crd::random_fill(v, generator, size); // 10 elements of type element_t
79
81
82 stream << "========== Serial Sort ==========" << nL;
83 for(size_t i = 0; i < test_count; ++i)
84 {
85 auto sorted_container = v;
86 sw.reset();
87 std::sort(sorted_container.begin(), sorted_container.end());
88 print_constainer(i, sorted_container, sw);
89 }
90
91 // if you don't flush, you cannot see anything
92 stream << flush;
93}
94
95int main()
96{
97 // examples_for_random_stopwatch();
99}
void examples_for_random_stopwatch()
tpf::sstream stream
auto nL
void print_constainer(size_t index, const ContainerType &container, const StopwatchType &sw)
auto flush
auto nl
void examples_for_serial_sort()
int main()
tpf::chrono_random::stop_watch stop_watch
void random_fill(Type(&container)[N], RandomGeneratorType const &random_generator)
Implements random number generator and stop watch.
constexpr new_line nl()
Definition: tpf_output.hpp:837
ContainerType< EleType > sort(ContainerType< EleType, Types... > container, sort_order order=sort_order::ascending, sort_method method=sort_method::size)
Definition: tpf_set.hpp:438
Stream output operators << are implemented.