|
| 1 | +/*************************************************************************** |
| 2 | + * Copyright (c) 2016, Johan Mabille, Sylvain Corlay and Wolf Vollprecht * |
| 3 | + * * |
| 4 | + * Distributed under the terms of the BSD 3-Clause License. * |
| 5 | + * * |
| 6 | + * The full license is in the file LICENSE, distributed with this software. * |
| 7 | + ****************************************************************************/ |
| 8 | + |
| 9 | +#include <benchmark/benchmark.h> |
| 10 | + |
| 11 | +#include "xtensor/containers/xtensor.hpp" |
| 12 | +#include "xtensor/core/xmath.hpp" |
| 13 | +#include "xtensor/generators/xrandom.hpp" |
| 14 | + |
| 15 | +namespace xt |
| 16 | +{ |
| 17 | + namespace |
| 18 | + { |
| 19 | + constexpr std::array<size_t, 2> cContainerAssignShape{2000, 2000}; |
| 20 | + |
| 21 | + template <class Shape> |
| 22 | + auto generateRandomInt16From0To100(Shape&& x) |
| 23 | + { |
| 24 | + return xt::random::randint(x, 0, 100); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + static void Xtensor_Uint16_2000x2000_DivideBy2_StdTransform(benchmark::State& aState) |
| 29 | + { |
| 30 | + xt::xtensor<uint16_t, 2> vInput = generateRandomInt16From0To100(cContainerAssignShape); |
| 31 | + auto vOutput = xt::xtensor<uint16_t, 2>::from_shape(cContainerAssignShape); |
| 32 | + |
| 33 | + for (auto _ : aState) |
| 34 | + { |
| 35 | + std::transform( |
| 36 | + vInput.begin(), |
| 37 | + vInput.end(), |
| 38 | + vOutput.begin(), |
| 39 | + [](auto&& aInputValue) |
| 40 | + { |
| 41 | + return aInputValue / 2; |
| 42 | + } |
| 43 | + ); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + static void Xtensor_Uint16_2000x2000_DivideBy2_Xtensor(benchmark::State& aState) |
| 48 | + { |
| 49 | + xt::xtensor<uint16_t, 2> vInput = generateRandomInt16From0To100(cContainerAssignShape); |
| 50 | + auto vOutput = xt::xtensor<uint16_t, 2>::from_shape(cContainerAssignShape); |
| 51 | + |
| 52 | + for (auto _ : aState) |
| 53 | + { |
| 54 | + vOutput = vInput / 2; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + static void Xtensor_Uint16_2000x2000_DivideBy2Double_StdTransform(benchmark::State& aState) |
| 59 | + { |
| 60 | + xt::xtensor<uint16_t, 2> vInput = generateRandomInt16From0To100(cContainerAssignShape); |
| 61 | + auto vOutput = xt::xtensor<uint16_t, 2>::from_shape(cContainerAssignShape); |
| 62 | + |
| 63 | + for (auto _ : aState) |
| 64 | + { |
| 65 | + std::transform( |
| 66 | + vInput.begin(), |
| 67 | + vInput.end(), |
| 68 | + vOutput.begin(), |
| 69 | + [](auto&& aInputValue) |
| 70 | + { |
| 71 | + return aInputValue / 2.0; |
| 72 | + } |
| 73 | + ); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + static void Xtensor_Uint16_2000x2000_DivideBy2Double_Xtensor(benchmark::State& aState) |
| 78 | + { |
| 79 | + xt::xtensor<uint16_t, 2> vInput = generateRandomInt16From0To100(cContainerAssignShape); |
| 80 | + auto vOutput = xt::xtensor<uint16_t, 2>::from_shape(cContainerAssignShape); |
| 81 | + |
| 82 | + for (auto _ : aState) |
| 83 | + { |
| 84 | + vOutput = vInput / 2.0; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + static void Xtensor_Uint16_2000x2000_MultiplyBy2_StdTransform(benchmark::State& aState) |
| 89 | + { |
| 90 | + xt::xtensor<uint16_t, 2> vInput = generateRandomInt16From0To100(cContainerAssignShape); |
| 91 | + auto vOutput = xt::xtensor<uint16_t, 2>::from_shape(cContainerAssignShape); |
| 92 | + |
| 93 | + for (auto _ : aState) |
| 94 | + { |
| 95 | + std::transform( |
| 96 | + vInput.begin(), |
| 97 | + vInput.end(), |
| 98 | + vOutput.begin(), |
| 99 | + [](auto&& aInputValue) |
| 100 | + { |
| 101 | + return aInputValue * 2; |
| 102 | + } |
| 103 | + ); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + static void Xtensor_Uint16_2000x2000_MultiplyBy2_Xtensor(benchmark::State& aState) |
| 108 | + { |
| 109 | + xt::xtensor<uint16_t, 2> vInput = generateRandomInt16From0To100(cContainerAssignShape); |
| 110 | + auto vOutput = xt::xtensor<uint16_t, 2>::from_shape(cContainerAssignShape); |
| 111 | + |
| 112 | + for (auto _ : aState) |
| 113 | + { |
| 114 | + vOutput = vInput * 2; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + static void Xtensor_Uint16_2000x2000_Maximum_StdTransform(benchmark::State& aState) |
| 119 | + { |
| 120 | + xt::xtensor<uint16_t, 2> vInput1 = generateRandomInt16From0To100(cContainerAssignShape); |
| 121 | + xt::xtensor<uint16_t, 2> vInput2 = generateRandomInt16From0To100(cContainerAssignShape); |
| 122 | + auto vOutput = xt::xtensor<uint16_t, 2>::from_shape(cContainerAssignShape); |
| 123 | + |
| 124 | + for (auto _ : aState) |
| 125 | + { |
| 126 | + auto vInput2It = vInput2.begin(); |
| 127 | + std::transform( |
| 128 | + vInput1.begin(), |
| 129 | + vInput1.end(), |
| 130 | + vOutput.begin(), |
| 131 | + [&vInput2It](auto&& aInput1Value) |
| 132 | + { |
| 133 | + return std::max(aInput1Value, *vInput2It++); |
| 134 | + } |
| 135 | + ); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + static void Xtensor_Uint16_2000x2000_Maximum_Xtensor(benchmark::State& aState) |
| 140 | + { |
| 141 | + xt::xtensor<uint16_t, 2> vInput1 = generateRandomInt16From0To100(cContainerAssignShape); |
| 142 | + xt::xtensor<uint16_t, 2> vInput2 = generateRandomInt16From0To100(cContainerAssignShape); |
| 143 | + auto vOutput = xt::xtensor<uint16_t, 2>::from_shape(cContainerAssignShape); |
| 144 | + |
| 145 | + for (auto _ : aState) |
| 146 | + { |
| 147 | + vOutput = xt::maximum(vInput1, vInput2); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + BENCHMARK(Xtensor_Uint16_2000x2000_Maximum_Xtensor); |
| 152 | + BENCHMARK(Xtensor_Uint16_2000x2000_Maximum_StdTransform); |
| 153 | + BENCHMARK(Xtensor_Uint16_2000x2000_MultiplyBy2_Xtensor); |
| 154 | + BENCHMARK(Xtensor_Uint16_2000x2000_MultiplyBy2_StdTransform); |
| 155 | + BENCHMARK(Xtensor_Uint16_2000x2000_DivideBy2Double_Xtensor); |
| 156 | + BENCHMARK(Xtensor_Uint16_2000x2000_DivideBy2Double_StdTransform); |
| 157 | +} |
0 commit comments