File: benchmark_stl.cpp

package info (click to toggle)
xtensor 0.27.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,528 kB
  • sloc: cpp: 65,746; makefile: 202; python: 171; javascript: 8
file content (157 lines) | stat: -rw-r--r-- 5,484 bytes parent folder | download
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/***************************************************************************
 * Copyright (c) 2016, Johan Mabille, Sylvain Corlay and Wolf Vollprecht    *
 *                                                                          *
 * Distributed under the terms of the BSD 3-Clause License.                 *
 *                                                                          *
 * The full license is in the file LICENSE, distributed with this software. *
 ****************************************************************************/

#include <benchmark/benchmark.h>

#include "xtensor/containers/xtensor.hpp"
#include "xtensor/core/xmath.hpp"
#include "xtensor/generators/xrandom.hpp"

namespace xt
{
    namespace
    {
        constexpr std::array<size_t, 2> cContainerAssignShape{2000, 2000};

        template <class Shape>
        auto generateRandomInt16From0To100(Shape&& x)
        {
            return xt::random::randint(x, 0, 100);
        }
    }

    static void Xtensor_Uint16_2000x2000_DivideBy2_StdTransform(benchmark::State& aState)
    {
        xt::xtensor<uint16_t, 2> vInput = generateRandomInt16From0To100(cContainerAssignShape);
        auto vOutput = xt::xtensor<uint16_t, 2>::from_shape(cContainerAssignShape);

        for (auto _ : aState)
        {
            std::transform(
                vInput.begin(),
                vInput.end(),
                vOutput.begin(),
                [](auto&& aInputValue)
                {
                    return aInputValue / 2;
                }
            );
        }
    }

    static void Xtensor_Uint16_2000x2000_DivideBy2_Xtensor(benchmark::State& aState)
    {
        xt::xtensor<uint16_t, 2> vInput = generateRandomInt16From0To100(cContainerAssignShape);
        auto vOutput = xt::xtensor<uint16_t, 2>::from_shape(cContainerAssignShape);

        for (auto _ : aState)
        {
            vOutput = vInput / 2;
        }
    }

    static void Xtensor_Uint16_2000x2000_DivideBy2Double_StdTransform(benchmark::State& aState)
    {
        xt::xtensor<uint16_t, 2> vInput = generateRandomInt16From0To100(cContainerAssignShape);
        auto vOutput = xt::xtensor<uint16_t, 2>::from_shape(cContainerAssignShape);

        for (auto _ : aState)
        {
            std::transform(
                vInput.begin(),
                vInput.end(),
                vOutput.begin(),
                [](auto&& aInputValue)
                {
                    return aInputValue / 2.0;
                }
            );
        }
    }

    static void Xtensor_Uint16_2000x2000_DivideBy2Double_Xtensor(benchmark::State& aState)
    {
        xt::xtensor<uint16_t, 2> vInput = generateRandomInt16From0To100(cContainerAssignShape);
        auto vOutput = xt::xtensor<uint16_t, 2>::from_shape(cContainerAssignShape);

        for (auto _ : aState)
        {
            vOutput = vInput / 2.0;
        }
    }

    static void Xtensor_Uint16_2000x2000_MultiplyBy2_StdTransform(benchmark::State& aState)
    {
        xt::xtensor<uint16_t, 2> vInput = generateRandomInt16From0To100(cContainerAssignShape);
        auto vOutput = xt::xtensor<uint16_t, 2>::from_shape(cContainerAssignShape);

        for (auto _ : aState)
        {
            std::transform(
                vInput.begin(),
                vInput.end(),
                vOutput.begin(),
                [](auto&& aInputValue)
                {
                    return aInputValue * 2;
                }
            );
        }
    }

    static void Xtensor_Uint16_2000x2000_MultiplyBy2_Xtensor(benchmark::State& aState)
    {
        xt::xtensor<uint16_t, 2> vInput = generateRandomInt16From0To100(cContainerAssignShape);
        auto vOutput = xt::xtensor<uint16_t, 2>::from_shape(cContainerAssignShape);

        for (auto _ : aState)
        {
            vOutput = vInput * 2;
        }
    }

    static void Xtensor_Uint16_2000x2000_Maximum_StdTransform(benchmark::State& aState)
    {
        xt::xtensor<uint16_t, 2> vInput1 = generateRandomInt16From0To100(cContainerAssignShape);
        xt::xtensor<uint16_t, 2> vInput2 = generateRandomInt16From0To100(cContainerAssignShape);
        auto vOutput = xt::xtensor<uint16_t, 2>::from_shape(cContainerAssignShape);

        for (auto _ : aState)
        {
            auto vInput2It = vInput2.begin();
            std::transform(
                vInput1.begin(),
                vInput1.end(),
                vOutput.begin(),
                [&vInput2It](auto&& aInput1Value)
                {
                    return std::max(aInput1Value, *vInput2It++);
                }
            );
        }
    }

    static void Xtensor_Uint16_2000x2000_Maximum_Xtensor(benchmark::State& aState)
    {
        xt::xtensor<uint16_t, 2> vInput1 = generateRandomInt16From0To100(cContainerAssignShape);
        xt::xtensor<uint16_t, 2> vInput2 = generateRandomInt16From0To100(cContainerAssignShape);
        auto vOutput = xt::xtensor<uint16_t, 2>::from_shape(cContainerAssignShape);

        for (auto _ : aState)
        {
            vOutput = xt::maximum(vInput1, vInput2);
        }
    }

    BENCHMARK(Xtensor_Uint16_2000x2000_Maximum_Xtensor);
    BENCHMARK(Xtensor_Uint16_2000x2000_Maximum_StdTransform);
    BENCHMARK(Xtensor_Uint16_2000x2000_MultiplyBy2_Xtensor);
    BENCHMARK(Xtensor_Uint16_2000x2000_MultiplyBy2_StdTransform);
    BENCHMARK(Xtensor_Uint16_2000x2000_DivideBy2Double_Xtensor);
    BENCHMARK(Xtensor_Uint16_2000x2000_DivideBy2Double_StdTransform);
}