Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cpp/src/arrow/compute/kernels/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
2 changes: 0 additions & 2 deletions cpp/src/arrow/compute/kernels/scalar_arithmetic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ struct Multiply {
static_assert(std::is_same<decltype(int16_t() * int16_t()), int32_t>::value, "");
static_assert(std::is_same<decltype(uint16_t() * uint16_t()), int32_t>::value, "");
static_assert(std::is_same<decltype(int32_t() * int32_t()), int32_t>::value, "");

static_assert(std::is_same<decltype(uint32_t() * uint32_t()), uint32_t>::value, "");

static_assert(std::is_same<decltype(int64_t() * int64_t()), int64_t>::value, "");
static_assert(std::is_same<decltype(uint64_t() * uint64_t()), uint64_t>::value, "");

Expand Down
102 changes: 102 additions & 0 deletions cpp/src/arrow/compute/kernels/scalar_arithmetic_benchmark.cc
Original file line number Diff line number Diff line change
@@ -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 <vector>

#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<Datum>(const Datum&, const Datum&, ExecContext*);

template <BinaryOp& Op, typename ArrowType, typename CType = typename ArrowType::c_type>
static void ArrayScalarKernel(benchmark::State& state) {
RegressionArgs args(state);

const int64_t array_size = args.size / sizeof(CType);
auto min = std::numeric_limits<CType>::lowest();
auto max = std::numeric_limits<CType>::max();

auto rand = random::RandomArrayGenerator(kSeed);
auto lhs = std::static_pointer_cast<NumericArray<ArrowType>>(
rand.Numeric<ArrowType>(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 <BinaryOp& Op, typename ArrowType, typename CType = typename ArrowType::c_type>
static void ArrayArrayKernel(benchmark::State& state) {
RegressionArgs args(state);

const int64_t array_size = args.size / sizeof(CType);
auto min = std::numeric_limits<CType>::lowest();
auto max = std::numeric_limits<CType>::max();

auto rand = random::RandomArrayGenerator(kSeed);
auto lhs = std::static_pointer_cast<NumericArray<ArrowType>>(
rand.Numeric<ArrowType>(array_size, min, max, args.null_proportion));
auto rhs = std::static_pointer_cast<NumericArray<ArrowType>>(
rand.Numeric<ArrowType>(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}) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a processor with 22MB L3 cache so generating that much random data is quite expensive. If we want to benchmark arrays that big we should generate a smaller sample of random data and repeat/tile it to make the bigger array.

for (const auto inverse_null_proportion : std::vector<ArgsType>({100, 0})) {
bench->Args({static_cast<ArgsType>(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