diff --git a/cpp/src/arrow/compute/kernels/CMakeLists.txt b/cpp/src/arrow/compute/kernels/CMakeLists.txt index 93b03e7871c..e3fa987fdaf 100644 --- a/cpp/src/arrow/compute/kernels/CMakeLists.txt +++ b/cpp/src/arrow/compute/kernels/CMakeLists.txt @@ -28,6 +28,7 @@ add_arrow_compute_test(scalar_test scalar_string_test.cc test_util.cc) +add_arrow_benchmark(scalar_arithmetic_benchmark PREFIX "arrow-compute") add_arrow_benchmark(scalar_compare_benchmark PREFIX "arrow-compute") add_arrow_benchmark(scalar_string_benchmark PREFIX "arrow-compute") diff --git a/cpp/src/arrow/compute/kernels/scalar_arithmetic.cc b/cpp/src/arrow/compute/kernels/scalar_arithmetic.cc index de05900f7c1..4459e4ffa35 100644 --- a/cpp/src/arrow/compute/kernels/scalar_arithmetic.cc +++ b/cpp/src/arrow/compute/kernels/scalar_arithmetic.cc @@ -83,9 +83,7 @@ struct Multiply { static_assert(std::is_same::value, ""); static_assert(std::is_same::value, ""); static_assert(std::is_same::value, ""); - static_assert(std::is_same::value, ""); - static_assert(std::is_same::value, ""); static_assert(std::is_same::value, ""); diff --git a/cpp/src/arrow/compute/kernels/scalar_arithmetic_benchmark.cc b/cpp/src/arrow/compute/kernels/scalar_arithmetic_benchmark.cc new file mode 100644 index 00000000000..34991b99819 --- /dev/null +++ b/cpp/src/arrow/compute/kernels/scalar_arithmetic_benchmark.cc @@ -0,0 +1,102 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "benchmark/benchmark.h" + +#include + +#include "arrow/compute/api_scalar.h" +#include "arrow/compute/benchmark_util.h" +#include "arrow/compute/kernels/test_util.h" +#include "arrow/testing/gtest_util.h" +#include "arrow/testing/random.h" + +namespace arrow { +namespace compute { + +constexpr auto kSeed = 0x94378165; + +using BinaryOp = Result(const Datum&, const Datum&, ExecContext*); + +template +static void ArrayScalarKernel(benchmark::State& state) { + RegressionArgs args(state); + + const int64_t array_size = args.size / sizeof(CType); + auto min = std::numeric_limits::lowest(); + auto max = std::numeric_limits::max(); + + auto rand = random::RandomArrayGenerator(kSeed); + auto lhs = std::static_pointer_cast>( + rand.Numeric(array_size, min, max, args.null_proportion)); + + Datum fifteen(CType(15)); + for (auto _ : state) { + ABORT_NOT_OK(Op(lhs, fifteen, nullptr).status()); + } + state.SetItemsProcessed(state.iterations() * array_size); +} + +template +static void ArrayArrayKernel(benchmark::State& state) { + RegressionArgs args(state); + + const int64_t array_size = args.size / sizeof(CType); + auto min = std::numeric_limits::lowest(); + auto max = std::numeric_limits::max(); + + auto rand = random::RandomArrayGenerator(kSeed); + auto lhs = std::static_pointer_cast>( + rand.Numeric(array_size, min, max, args.null_proportion)); + auto rhs = std::static_pointer_cast>( + rand.Numeric(array_size, min, max, args.null_proportion)); + + for (auto _ : state) { + ABORT_NOT_OK(Op(lhs, rhs, nullptr).status()); + } + state.SetItemsProcessed(state.iterations() * array_size); +} + +void SetArgs(benchmark::internal::Benchmark* bench) { + for (const auto size : {kL1Size, kL2Size}) { + for (const auto inverse_null_proportion : std::vector({100, 0})) { + bench->Args({static_cast(size), inverse_null_proportion}); + } + } +} + +#define DECLARE_ARITHMETIC_BENCHMARKS(BENCHMARK, OP) \ + BENCHMARK_TEMPLATE(BENCHMARK, OP, Int64Type)->Apply(SetArgs); \ + BENCHMARK_TEMPLATE(BENCHMARK, OP, Int32Type)->Apply(SetArgs); \ + BENCHMARK_TEMPLATE(BENCHMARK, OP, Int16Type)->Apply(SetArgs); \ + BENCHMARK_TEMPLATE(BENCHMARK, OP, Int8Type)->Apply(SetArgs); \ + BENCHMARK_TEMPLATE(BENCHMARK, OP, UInt64Type)->Apply(SetArgs); \ + BENCHMARK_TEMPLATE(BENCHMARK, OP, UInt32Type)->Apply(SetArgs); \ + BENCHMARK_TEMPLATE(BENCHMARK, OP, UInt16Type)->Apply(SetArgs); \ + BENCHMARK_TEMPLATE(BENCHMARK, OP, UInt8Type)->Apply(SetArgs); \ + BENCHMARK_TEMPLATE(BENCHMARK, OP, FloatType)->Apply(SetArgs); \ + BENCHMARK_TEMPLATE(BENCHMARK, OP, DoubleType)->Apply(SetArgs) + +DECLARE_ARITHMETIC_BENCHMARKS(ArrayArrayKernel, Add); +DECLARE_ARITHMETIC_BENCHMARKS(ArrayScalarKernel, Add); +DECLARE_ARITHMETIC_BENCHMARKS(ArrayArrayKernel, Subtract); +DECLARE_ARITHMETIC_BENCHMARKS(ArrayScalarKernel, Subtract); +DECLARE_ARITHMETIC_BENCHMARKS(ArrayArrayKernel, Multiply); +DECLARE_ARITHMETIC_BENCHMARKS(ArrayScalarKernel, Multiply); + +} // namespace compute +} // namespace arrow