]>
git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/cpp/src/arrow/util/decimal_benchmark.cc
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
18 #include "benchmark/benchmark.h"
23 #include "arrow/util/decimal.h"
24 #include "arrow/util/logging.h"
25 #include "arrow/util/macros.h"
30 static const std::vector
<std::string
>& GetValuesAsString() {
31 static const std::vector
<std::string
> kValues
= {"0",
35 "123456789.123456789",
36 "1231234567890.451234567890"};
40 struct DecimalValueAndScale
{
45 static std::vector
<DecimalValueAndScale
> GetDecimalValuesAndScales() {
46 const std::vector
<std::string
>& value_strs
= GetValuesAsString();
47 std::vector
<DecimalValueAndScale
> result(value_strs
.size());
48 for (size_t i
= 0; i
< value_strs
.size(); ++i
) {
50 ARROW_CHECK_OK(Decimal128::FromString(value_strs
[i
], &result
[i
].decimal
,
51 &result
[i
].scale
, &precision
));
56 static void FromString(benchmark::State
& state
) { // NOLINT non-const reference
57 const std::vector
<std::string
>& values
= GetValuesAsString();
58 for (auto _
: state
) {
59 for (const auto& value
: values
) {
61 int32_t scale
, precision
;
62 benchmark::DoNotOptimize(Decimal128::FromString(value
, &dec
, &scale
, &precision
));
65 state
.SetItemsProcessed(state
.iterations() * values
.size());
68 static void ToString(benchmark::State
& state
) { // NOLINT non-const reference
69 static const std::vector
<DecimalValueAndScale
> values
= GetDecimalValuesAndScales();
70 for (auto _
: state
) {
71 for (const DecimalValueAndScale
& item
: values
) {
72 benchmark::DoNotOptimize(item
.decimal
.ToString(item
.scale
));
75 state
.SetItemsProcessed(state
.iterations() * values
.size());
78 constexpr int32_t kValueSize
= 10;
80 static void BinaryCompareOp(benchmark::State
& state
) { // NOLINT non-const reference
81 std::vector
<BasicDecimal128
> v1
, v2
;
82 for (int x
= 0; x
< kValueSize
; x
++) {
83 v1
.emplace_back(100 + x
, 100 + x
);
84 v2
.emplace_back(200 + x
, 200 + x
);
86 for (auto _
: state
) {
87 for (int x
= 0; x
< kValueSize
; x
+= 4) {
88 benchmark::DoNotOptimize(v1
[x
] == v2
[x
]);
89 benchmark::DoNotOptimize(v1
[x
+ 1] <= v2
[x
+ 1]);
90 benchmark::DoNotOptimize(v1
[x
+ 2] >= v2
[x
+ 2]);
91 benchmark::DoNotOptimize(v1
[x
+ 3] >= v1
[x
+ 3]);
94 state
.SetItemsProcessed(state
.iterations() * kValueSize
);
97 static void BinaryCompareOpConstant(
98 benchmark::State
& state
) { // NOLINT non-const reference
99 std::vector
<BasicDecimal128
> v1
;
100 for (int x
= 0; x
< kValueSize
; x
++) {
101 v1
.emplace_back(100 + x
, 100 + x
);
103 BasicDecimal128
constant(313, 212);
104 for (auto _
: state
) {
105 for (int x
= 0; x
< kValueSize
; x
+= 4) {
106 benchmark::DoNotOptimize(v1
[x
] == constant
);
107 benchmark::DoNotOptimize(v1
[x
+ 1] <= constant
);
108 benchmark::DoNotOptimize(v1
[x
+ 2] >= constant
);
109 benchmark::DoNotOptimize(v1
[x
+ 3] != constant
);
112 state
.SetItemsProcessed(state
.iterations() * kValueSize
);
115 static void BinaryMathOpAggregate(
116 benchmark::State
& state
) { // NOLINT non-const reference
117 std::vector
<BasicDecimal128
> v
;
118 for (int x
= 0; x
< kValueSize
; x
++) {
119 v
.emplace_back(100 + x
, 100 + x
);
122 for (auto _
: state
) {
123 BasicDecimal128 result
;
124 for (int x
= 0; x
< 100; x
++) {
127 benchmark::DoNotOptimize(result
);
129 state
.SetItemsProcessed(state
.iterations() * kValueSize
);
132 static void BinaryMathOpAdd128(benchmark::State
& state
) { // NOLINT non-const reference
133 std::vector
<BasicDecimal128
> v1
, v2
;
134 for (int x
= 0; x
< kValueSize
; x
++) {
135 v1
.emplace_back(100 + x
, 100 + x
);
136 v2
.emplace_back(200 + x
, 200 + x
);
139 for (auto _
: state
) {
140 for (int x
= 0; x
< kValueSize
; ++x
) {
141 benchmark::DoNotOptimize(v1
[x
] + v2
[x
]);
144 state
.SetItemsProcessed(state
.iterations() * kValueSize
);
147 static void BinaryMathOpMultiply128(
148 benchmark::State
& state
) { // NOLINT non-const reference
149 std::vector
<BasicDecimal128
> v1
, v2
;
150 for (int x
= 0; x
< kValueSize
; x
++) {
151 v1
.emplace_back(100 + x
, 100 + x
);
152 v2
.emplace_back(200 + x
, 200 + x
);
155 for (auto _
: state
) {
156 for (int x
= 0; x
< kValueSize
; ++x
) {
157 benchmark::DoNotOptimize(v1
[x
] * v2
[x
]);
160 state
.SetItemsProcessed(state
.iterations() * kValueSize
);
163 static void BinaryMathOpDivide128(
164 benchmark::State
& state
) { // NOLINT non-const reference
165 std::vector
<BasicDecimal128
> v1
, v2
;
166 for (int x
= 0; x
< kValueSize
; x
++) {
167 v1
.emplace_back(100 + x
, 100 + x
);
168 v2
.emplace_back(200 + x
, 200 + x
);
171 for (auto _
: state
) {
172 for (int x
= 0; x
< kValueSize
; ++x
) {
173 benchmark::DoNotOptimize(v1
[x
] / v2
[x
]);
176 state
.SetItemsProcessed(state
.iterations() * kValueSize
);
179 static void BinaryMathOpAdd256(benchmark::State
& state
) { // NOLINT non-const reference
180 std::vector
<BasicDecimal256
> v1
, v2
;
181 for (uint64_t x
= 0; x
< kValueSize
; x
++) {
182 v1
.push_back(BasicDecimal256({100 + x
, 100 + x
, 100 + x
, 100 + x
}));
183 v2
.push_back(BasicDecimal256({200 + x
, 200 + x
, 200 + x
, 200 + x
}));
186 for (auto _
: state
) {
187 for (int x
= 0; x
< kValueSize
; ++x
) {
188 benchmark::DoNotOptimize(v1
[x
] + v2
[x
]);
191 state
.SetItemsProcessed(state
.iterations() * kValueSize
);
194 static void BinaryMathOpMultiply256(
195 benchmark::State
& state
) { // NOLINT non-const reference
196 std::vector
<BasicDecimal256
> v1
, v2
;
197 for (uint64_t x
= 0; x
< kValueSize
; x
++) {
198 v1
.push_back(BasicDecimal256({100 + x
, 100 + x
, 100 + x
, 100 + x
}));
199 v2
.push_back(BasicDecimal256({200 + x
, 200 + x
, 200 + x
, 200 + x
}));
202 for (auto _
: state
) {
203 for (int x
= 0; x
< kValueSize
; ++x
) {
204 benchmark::DoNotOptimize(v1
[x
] * v2
[x
]);
207 state
.SetItemsProcessed(state
.iterations() * kValueSize
);
210 static void BinaryMathOpDivide256(
211 benchmark::State
& state
) { // NOLINT non-const reference
212 std::vector
<BasicDecimal256
> v1
, v2
;
213 for (uint64_t x
= 0; x
< kValueSize
; x
++) {
214 v1
.push_back(BasicDecimal256({100 + x
, 100 + x
, 100 + x
, 100 + x
}));
215 v2
.push_back(BasicDecimal256({200 + x
, 200 + x
, 200 + x
, 200 + x
}));
218 for (auto _
: state
) {
219 for (int x
= 0; x
< kValueSize
; ++x
) {
220 benchmark::DoNotOptimize(v1
[x
] / v2
[x
]);
223 state
.SetItemsProcessed(state
.iterations() * kValueSize
);
226 static void UnaryOp(benchmark::State
& state
) { // NOLINT non-const reference
227 std::vector
<BasicDecimal128
> v
;
228 for (int x
= 0; x
< kValueSize
; x
++) {
229 v
.emplace_back(100 + x
, 100 + x
);
232 for (auto _
: state
) {
233 for (int x
= 0; x
< kValueSize
; x
+= 2) {
234 benchmark::DoNotOptimize(v
[x
].Abs());
235 benchmark::DoNotOptimize(v
[x
+ 1].Negate());
238 state
.SetItemsProcessed(state
.iterations() * kValueSize
);
241 static void Constants(benchmark::State
& state
) { // NOLINT non-const reference
242 BasicDecimal128
d1(-546, 123), d2(-123, 456);
243 for (auto _
: state
) {
244 benchmark::DoNotOptimize(BasicDecimal128::GetMaxValue() - d1
);
245 benchmark::DoNotOptimize(BasicDecimal128::GetScaleMultiplier(3) + d2
);
247 state
.SetItemsProcessed(state
.iterations() * 2);
250 static void BinaryBitOp(benchmark::State
& state
) { // NOLINT non-const reference
251 std::vector
<BasicDecimal128
> v1
, v2
;
252 for (int x
= 0; x
< kValueSize
; x
++) {
253 v1
.emplace_back(100 + x
, 100 + x
);
254 v2
.emplace_back(200 + x
, 200 + x
);
257 for (auto _
: state
) {
258 for (int x
= 0; x
< kValueSize
; x
+= 2) {
259 benchmark::DoNotOptimize(v1
[x
] |= v2
[x
]);
260 benchmark::DoNotOptimize(v1
[x
+ 1] &= v2
[x
+ 1]);
263 state
.SetItemsProcessed(state
.iterations() * kValueSize
);
266 BENCHMARK(FromString
);
268 BENCHMARK(BinaryMathOpAdd128
);
269 BENCHMARK(BinaryMathOpMultiply128
);
270 BENCHMARK(BinaryMathOpDivide128
);
271 BENCHMARK(BinaryMathOpAdd256
);
272 BENCHMARK(BinaryMathOpMultiply256
);
273 BENCHMARK(BinaryMathOpDivide256
);
274 BENCHMARK(BinaryMathOpAggregate
);
275 BENCHMARK(BinaryCompareOp
);
276 BENCHMARK(BinaryCompareOpConstant
);
278 BENCHMARK(Constants
);
279 BENCHMARK(BinaryBitOp
);
281 } // namespace Decimal