|
| 1 | + |
| 2 | + |
| 3 | +#include <algorithm> |
| 4 | +#include <chrono> |
| 5 | +#include <execution> |
| 6 | +#include <iostream> |
| 7 | +#include <numeric> |
| 8 | +#include <vector> |
| 9 | +template <typename TFunc> |
| 10 | +void RunAndMeasure(const char* title, TFunc func) { |
| 11 | + const auto start = std::chrono::steady_clock::now(); |
| 12 | + auto ret = func(); |
| 13 | + const auto end = std::chrono::steady_clock::now(); |
| 14 | + std::cout << title << ": " |
| 15 | + << std::chrono::duration<double, std::milli>(end - start).count() |
| 16 | + << " ms, res " << ret << "\n"; |
| 17 | +} |
| 18 | + |
| 19 | +int main() { |
| 20 | + int size=1024000000; |
| 21 | + std::vector<double> v(1024000000, 0.5); |
| 22 | + std::vector<double> result(v.size()); |
| 23 | + |
| 24 | + std::vector<double> v1(size); |
| 25 | + std::iota(v1.begin(), v1.end(), 1.0); |
| 26 | + |
| 27 | + |
| 28 | + RunAndMeasure("std::warm up", [&v] { |
| 29 | + return std::reduce(std::execution::seq, v.begin(), v.end(), 0.0); |
| 30 | + }); |
| 31 | + |
| 32 | + RunAndMeasure("std::accumulate", |
| 33 | + [&v] { return std::accumulate(v.begin(), v.end(), 0.0); }); |
| 34 | + |
| 35 | + RunAndMeasure("std::reduce, seq", [&v] { |
| 36 | + return std::reduce(std::execution::seq, v.begin(), v.end(), 0.0); |
| 37 | + }); |
| 38 | + |
| 39 | + RunAndMeasure("std::reduce, par", [&v] { |
| 40 | + return std::reduce(std::execution::par, v.begin(), v.end(), 0.0); |
| 41 | + }); |
| 42 | + |
| 43 | + RunAndMeasure("std::reduce, par_unseq", [&v] { |
| 44 | + return std::reduce(std::execution::par_unseq, v.begin(), v.end(), 0.0); |
| 45 | + }); |
| 46 | + |
| 47 | + |
| 48 | + RunAndMeasure("std::find, seq", [&v] { |
| 49 | + auto res = std::find(std::execution::seq, std::begin(v), std::end(v), 0.6); |
| 50 | + return res == std::end(v) ? 0.0 : 1.0; |
| 51 | + }); |
| 52 | + |
| 53 | + RunAndMeasure("std::find, par", [&v] { |
| 54 | + auto res = std::find(std::execution::par, std::begin(v), std::end(v), 0.6); |
| 55 | + return res == std::end(v) ? 0.0 : 1.0; |
| 56 | + }); |
| 57 | + |
| 58 | + RunAndMeasure("std::find, par_unseq", [&v] { |
| 59 | + auto res = std::find(std::execution::par_unseq, std::begin(v), std::end(v), 0.6); |
| 60 | + return res == std::end(v) ? 0.0 : 1.0; |
| 61 | + }); |
| 62 | + RunAndMeasure("std::copy_if, seq", [&v, &result] { |
| 63 | + auto new_end = std::copy_if(std::execution::seq, v.begin(), v.end(), result.begin(), |
| 64 | + [](double value) { return value > 0.4; }); |
| 65 | + return std::distance(result.begin(), new_end); |
| 66 | + }); |
| 67 | + |
| 68 | + RunAndMeasure("std::copy_if, par", [&v, &result] { |
| 69 | + auto new_end = std::copy_if(std::execution::par, v.begin(), v.end(), result.begin(), |
| 70 | + [](double value) { return value > 0.4; }); |
| 71 | + return std::distance(result.begin(), new_end); |
| 72 | + }); |
| 73 | + |
| 74 | + RunAndMeasure("std::copy_if, par_unseq", [&v, &result] { |
| 75 | + auto new_end = std::copy_if(std::execution::par_unseq, v.begin(), v.end(), result.begin(), |
| 76 | + [](double value) { return value > 0.4; }); |
| 77 | + return std::distance(result.begin(), new_end); |
| 78 | + }); |
| 79 | + |
| 80 | + RunAndMeasure("std::inclusive_scan, seq", [&v] { |
| 81 | + std::vector<double> scan_result(v.size()); |
| 82 | + std::inclusive_scan(std::execution::seq, v.begin(), v.end(), scan_result.begin()); |
| 83 | + return scan_result.back(); |
| 84 | + }); |
| 85 | + |
| 86 | + RunAndMeasure("std::inclusive_scan, par", [&v] { |
| 87 | + std::vector<double> scan_result(v.size()); |
| 88 | + std::inclusive_scan(std::execution::par, v.begin(), v.end(), scan_result.begin()); |
| 89 | + return scan_result.back(); |
| 90 | + }); |
| 91 | + |
| 92 | + RunAndMeasure("std::inclusive_scan, par_unseq", [&v] { |
| 93 | + std::vector<double> scan_result(v.size()); |
| 94 | + std::inclusive_scan(std::execution::par_unseq, v.begin(), v.end(), scan_result.begin()); |
| 95 | + return scan_result.back(); |
| 96 | + }); |
| 97 | + |
| 98 | + |
| 99 | + RunAndMeasure("std::min_element, seq", [&v1] { |
| 100 | + return *std::min_element(std::execution::seq, v1.begin(), v1.end()); |
| 101 | + }); |
| 102 | + |
| 103 | + RunAndMeasure("std::min_element, par", [&v1] { |
| 104 | + return *std::min_element(std::execution::par, v1.begin(), v1.end()); |
| 105 | + }); |
| 106 | + |
| 107 | + RunAndMeasure("std::min_element, par_unseq", [&v1] { |
| 108 | + return *std::min_element(std::execution::par_unseq, v1.begin(), v1.end()); |
| 109 | + }); |
| 110 | + |
| 111 | + RunAndMeasure("std::max_element, seq", [&v1] { |
| 112 | + return *std::max_element(std::execution::seq, v1.begin(), v1.end()); |
| 113 | + }); |
| 114 | + |
| 115 | + RunAndMeasure("std::max_element, par", [&v1] { |
| 116 | + return *std::max_element(std::execution::par, v1.begin(), v1.end()); |
| 117 | + }); |
| 118 | + |
| 119 | + RunAndMeasure("std::max_element, par_unseq", [&v1] { |
| 120 | + return *std::max_element(std::execution::par_unseq, v1.begin(), v1.end()); |
| 121 | + }); |
| 122 | + |
| 123 | + RunAndMeasure("std::minmax_element, seq", [&v1] { |
| 124 | + auto result = std::minmax_element(std::execution::seq, v1.begin(), v1.end()); |
| 125 | + return *result.first + *result.second; |
| 126 | + }); |
| 127 | + |
| 128 | + RunAndMeasure("std::minmax_element, par", [&v1] { |
| 129 | + auto result = std::minmax_element(std::execution::par, v1.begin(), v1.end()); |
| 130 | + return *result.first + *result.second; |
| 131 | + }); |
| 132 | + |
| 133 | + RunAndMeasure("std::minmax_element, par_unseq", [&v1] { |
| 134 | + auto result = std::minmax_element(std::execution::par_unseq, v1.begin(), v1.end()); |
| 135 | + return *result.first + *result.second; |
| 136 | + }); |
| 137 | + |
| 138 | + RunAndMeasure("std::is_partitioned, seq", [&v] { |
| 139 | + return std::is_partitioned(std::execution::seq, v.begin(), v.end(), [](double n) { return n < 1.0; }); |
| 140 | + }); |
| 141 | + |
| 142 | + RunAndMeasure("std::is_partitioned, par", [&v] { |
| 143 | + return std::is_partitioned(std::execution::par, v.begin(), v.end(), [](double n) { return n < 1.0; }); |
| 144 | + }); |
| 145 | + |
| 146 | + RunAndMeasure("std::is_partitioned, par_unseq", [&v] { |
| 147 | + return std::is_partitioned(std::execution::par_unseq, v.begin(), v.end(), [](double n) { return n < 1.0; }); |
| 148 | + }); |
| 149 | + |
| 150 | + RunAndMeasure("std::lexicographical_compare, seq", [&v] { |
| 151 | + std::vector<double> v2(1024000000, 0.5); |
| 152 | + return std::lexicographical_compare(std::execution::seq, v.begin(), v.end(), v2.begin(), v2.end()); |
| 153 | + }); |
| 154 | + |
| 155 | + RunAndMeasure("std::lexicographical_compare, par", [&v] { |
| 156 | + std::vector<double> v2(1024000000, 0.5); |
| 157 | + return std::lexicographical_compare(std::execution::par, v.begin(), v.end(), v2.begin(), v2.end()); |
| 158 | + }); |
| 159 | + |
| 160 | + RunAndMeasure("std::lexicographical_compare, par_unseq", [&v] { |
| 161 | + std::vector<double> v2(1024000000, 0.5); |
| 162 | + return std::lexicographical_compare(std::execution::par_unseq, v.begin(), v.end(), v2.begin(), v2.end()); |
| 163 | + }); |
| 164 | + |
| 165 | + RunAndMeasure("std::binary_search", [&v] { |
| 166 | + return std::binary_search( v.begin(), v.end(), 0.5); |
| 167 | + }); |
| 168 | + |
| 169 | + RunAndMeasure("std::lower_bound", [&v1] { |
| 170 | + return *std::lower_bound(v1.begin(), v1.end(), 0.5); |
| 171 | + }); |
| 172 | + |
| 173 | + RunAndMeasure("std::upper_bound", [&v1] { |
| 174 | + return *std::upper_bound( v1.begin(), v1.end(), 0.5); |
| 175 | + }); |
| 176 | + |
| 177 | + return 0; |
| 178 | +} |
0 commit comments