Created
June 27, 2021 20:43
-
-
Save ttsugriy/e287780bf5a5384825368b20115e3104 to your computer and use it in GitHub Desktop.
Benchmark Transform Reduce algo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <numeric> | |
| #include <vector> | |
| #include <execution> | |
| const int N = 1000000; | |
| std::vector<double> xs(N, 1.1); | |
| std::vector<double> ys(N, 1.1); | |
| double inner_product_1(const std::vector<double>& xs, const std::vector<double>& ys) { | |
| return std::inner_product(xs.cbegin(), xs.cend(), ys.cbegin(), 0); | |
| } | |
| template <class P> | |
| double inner_product_2(P&& policy, const std::vector<double>& xs, const std::vector<double>& ys) { | |
| return std::transform_reduce(policy, xs.cbegin(), xs.cend(), ys.cbegin(), 0); | |
| } | |
| double inner_product_3(const std::vector<double>& xs, const std::vector<double>& ys) { | |
| return std::transform_reduce(xs.cbegin(), xs.cend(), ys.cbegin(), 0); | |
| } | |
| static void bench_inner_product_1(benchmark::State& state) { | |
| double total = 0; | |
| for (auto _ : state) { | |
| benchmark::DoNotOptimize(total += inner_product_1(xs, ys)); | |
| } | |
| } | |
| BENCHMARK(bench_inner_product_1); | |
| static void bench_inner_product_2(benchmark::State& state) { | |
| double total = 0; | |
| for (auto _ : state) { | |
| benchmark::DoNotOptimize(total += inner_product_2(std::execution::seq, xs, ys)); | |
| } | |
| } | |
| BENCHMARK(bench_inner_product_2); | |
| static void bench_inner_product_3(benchmark::State& state) { | |
| double total = 0; | |
| for (auto _ : state) { | |
| benchmark::DoNotOptimize(total += inner_product_2(std::execution::par, xs, ys)); | |
| } | |
| } | |
| BENCHMARK(bench_inner_product_3); | |
| static void bench_inner_product_4(benchmark::State& state) { | |
| double total = 0; | |
| for (auto _ : state) { | |
| benchmark::DoNotOptimize(total += inner_product_2(std::execution::par_unseq, xs, ys)); | |
| } | |
| } | |
| BENCHMARK(bench_inner_product_4); | |
| static void bench_inner_product_5(benchmark::State& state) { | |
| double total = 0; | |
| for (auto _ : state) { | |
| benchmark::DoNotOptimize(total += inner_product_2(std::execution::unseq, xs, ys)); | |
| } | |
| } | |
| BENCHMARK(bench_inner_product_5); | |
| static void bench_inner_product_6(benchmark::State& state) { | |
| double total = 0; | |
| for (auto _ : state) { | |
| benchmark::DoNotOptimize(total += inner_product_3(xs, ys)); | |
| } | |
| } | |
| BENCHMARK(bench_inner_product_6); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment