diff --git a/cpp/src/arrow/CMakeLists.txt b/cpp/src/arrow/CMakeLists.txt index 3d1b621db0e9..4bf1008af4cd 100644 --- a/cpp/src/arrow/CMakeLists.txt +++ b/cpp/src/arrow/CMakeLists.txt @@ -1175,6 +1175,7 @@ add_arrow_benchmark(builder_benchmark) add_arrow_benchmark(compare_benchmark) add_arrow_benchmark(memory_pool_benchmark) add_arrow_benchmark(type_benchmark) +add_arrow_benchmark(tensor_benchmark) # # Recurse into sub-directories diff --git a/cpp/src/arrow/tensor_benchmark.cc b/cpp/src/arrow/tensor_benchmark.cc new file mode 100644 index 000000000000..91a9270ef347 --- /dev/null +++ b/cpp/src/arrow/tensor_benchmark.cc @@ -0,0 +1,68 @@ +// 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 "arrow/record_batch.h" +#include "arrow/testing/gtest_util.h" +#include "arrow/testing/random.h" +#include "arrow/type.h" +#include "arrow/util/benchmark_util.h" + +namespace arrow { + +template +static void BatchToTensorSimple(benchmark::State& state) { + using CType = typename ValueType::c_type; + std::shared_ptr ty = TypeTraits::type_singleton(); + + const int64_t num_cols = state.range(1); + const int64_t num_rows = state.range(0) / num_cols / sizeof(CType); + arrow::random::RandomArrayGenerator gen_{42}; + + std::vector> fields = {}; + std::vector> columns = {}; + + for (int64_t i = 0; i < num_cols; ++i) { + fields.push_back(field("f" + std::to_string(i), ty)); + columns.push_back(gen_.ArrayOf(ty, num_rows)); + } + auto schema = std::make_shared(std::move(fields)); + auto batch = RecordBatch::Make(schema, num_rows, columns); + + for (auto _ : state) { + ASSERT_OK_AND_ASSIGN(auto tensor, batch->ToTensor()); + } + state.SetItemsProcessed(state.iterations() * num_rows * num_cols); + state.SetBytesProcessed(state.iterations() * ty->byte_width() * num_rows * num_cols); +} + +void SetArgs(benchmark::internal::Benchmark* bench) { + for (int64_t size : {kL1Size, kL2Size}) { + for (int64_t num_columns : {3, 30, 300}) { + bench->Args({size, num_columns}); + bench->ArgNames({"size", "num_columns"}); + } + } +} + +BENCHMARK_TEMPLATE(BatchToTensorSimple, Int8Type)->Apply(SetArgs); +BENCHMARK_TEMPLATE(BatchToTensorSimple, Int16Type)->Apply(SetArgs); +BENCHMARK_TEMPLATE(BatchToTensorSimple, Int32Type)->Apply(SetArgs); +BENCHMARK_TEMPLATE(BatchToTensorSimple, Int64Type)->Apply(SetArgs); + +} // namespace arrow