cyb70289 commented on code in PR #15201: URL: https://github.com/apache/arrow/pull/15201#discussion_r1065303435
########## cpp/src/arrow/compute/kernels/scalar_round_benchmark.cc: ########## @@ -0,0 +1,125 @@ +// 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/kernels/test_util.h" +#include "arrow/testing/gtest_util.h" +#include "arrow/testing/random.h" +#include "arrow/util/benchmark_util.h" + +namespace arrow { +namespace compute { + +// Use a fixed hash to ensure consistent results from run to run. +constexpr auto kSeed = 0x94378165; + +template <typename ArrowType, RoundMode Mode, typename CType = typename ArrowType::c_type> +static void RoundArrayBenchmark(benchmark::State& state, const std::string& func_name) { + RegressionArgs args(state); + + const int64_t array_size = args.size / sizeof(CType); + auto rand = random::RandomArrayGenerator(kSeed); + + // Choose values so as to avoid overflow on all ops and types. + auto min = static_cast<CType>(6); + auto max = static_cast<CType>(min + 15); + auto val = std::static_pointer_cast<NumericArray<ArrowType>>( + rand.Numeric<ArrowType>(array_size, min, max, args.null_proportion)); + RoundOptions options; + options.round_mode = static_cast<RoundMode>(Mode); + + for (auto _ : state) { + ABORT_NOT_OK(CallFunction(func_name, {val}, &options)); + } + state.SetItemsProcessed(state.iterations() * array_size); +} + +void SetRoundArgs(benchmark::internal::Benchmark* bench) { + bench->ArgNames({"size", "inverse_null_proportion"}); + + for (const auto inverse_null_proportion : std::vector<ArgsType>({100, 0})) { + bench->Args({static_cast<ArgsType>(kL2Size), inverse_null_proportion}); + } +} + +template <typename ArrowType, RoundMode Mode> +static void Ceil(benchmark::State& state) { + RoundArrayBenchmark<ArrowType, Mode>(state, "ceil"); +} + +template <typename ArrowType, RoundMode Mode> +static void Floor(benchmark::State& state) { + RoundArrayBenchmark<FloatType, Mode>(state, "floor"); Review Comment: We can leave decimals as followup task. Can you open an issue to track it? Thanks. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
