Index: libcxx/benchmarks/algorithms.bench.cpp =================================================================== --- libcxx/benchmarks/algorithms.bench.cpp +++ libcxx/benchmarks/algorithms.bench.cpp @@ -1,3 +1,10 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// #include #include Index: libcxx/benchmarks/algorithms.merge.bench.cpp =================================================================== --- /dev/null +++ libcxx/benchmarks/algorithms.merge.bench.cpp @@ -0,0 +1,149 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include +#include + +#include "benchmark/benchmark.h" + +#include "CartesianBenchmarks.h" +#include "GenerateInput.h" +#include "test_macros.h" + +namespace { + +constexpr size_t kSplitPointCount = 5; + +template +std::array pickSplitPoints(I f, I l) { + std::ptrdiff_t N = std::distance(f, l); + + return {f + N / 4, f + N / 3, f + N / 2, f + 2 * N / 3, f + 3 * N / 4}; +} + +template +struct TestIntBase { + static std::vector generateInput(size_t size) { + std::vector Res(size); + std::generate(Res.begin(), Res.end(), + [] { return getRandomInteger(); }); + return Res; + } +}; + +struct TestInt32 : TestIntBase { + static constexpr const char* Name = "TestInt32"; +}; + +struct TestInt64 : TestIntBase { + static constexpr const char* Name = "TestInt64"; +}; + +struct TestUint32 : TestIntBase { + static constexpr const char* Name = "TestUint32"; +}; + +struct TestMediumString { + static constexpr const char* Name = "TestMediumString"; + static constexpr size_t StringSize = 32; + + static std::vector generateInput(size_t size) { + std::vector Res(size); + std::generate(Res.begin(), Res.end(), + [] { return getRandomString(StringSize); }); + return Res; + } +}; + +using AllTestTypes = + std::tuple; + +struct MergeAlg { + template + O operator()(I1 first1, I1 last1, I2 first2, I2 last2, O out) { + return std::merge(first1, last1, first2, last2, out); + } + + static constexpr const char* Name = "MergeAlg"; +}; + +struct SetUnionAlg { + template + O operator()(I1 first1, I1 last1, I2 first2, I2 last2, O out) { + return std::set_union(first1, last1, first2, last2, out); + } + + static constexpr const char* Name = "SetUnionAlg"; +}; + +using AllAlgs = std::tuple; + +} // namespace + +template +struct MergeBench { + size_t Quantity; + + std::string name() const { + return std::string("MergeBench_") + Alg::Name + "_" + TestType::Name + '/' + + std::to_string(Quantity); + } + + template + struct BenchmarkPrepared { + std::array LhsArray; + std::array RhsArray; + VecT output; + }; + + TEST_NOINLINE auto prepareBenchmark() const { + auto Data = TestType::generateInput(Quantity); + using VecT = decltype(Data); + + const auto SplitPoints = pickSplitPoints(Data.begin(), Data.end()); + + BenchmarkPrepared Res; + + VecT* LhsIt = Res.LhsArray.begin(); + VecT* RhsIt = Res.RhsArray.begin(); + for (auto Split : SplitPoints) { + *LhsIt = VecT{Data.begin(), Split}; + *RhsIt = VecT{Split, Data.end()}; + std::sort(LhsIt->begin(), LhsIt->end()); + std::sort(RhsIt->begin(), RhsIt->end()); + ++LhsIt; + ++RhsIt; + } + + Res.output = VecT(Data.size()); + + return Res; + } + + void run(benchmark::State& state) const { + auto [LhsArray, RhsArray, Output] = prepareBenchmark(); + + for (auto _ : state) { + for (size_t i = 0; i < LhsArray.size(); ++i) { + Alg{}(LhsArray[i].begin(), LhsArray[i].end(), RhsArray[i].begin(), + RhsArray[i].end(), Output.begin()); + benchmark::DoNotOptimize(Output); + } + } + } +}; + +int main(int argc, char** argv) { + benchmark::Initialize(&argc, argv); + if (benchmark::ReportUnrecognizedArguments(argc, argv)) + return 1; + + const std::vector Quantities = {1 << 9, 1 << 11, 1 << 21}; + makeCartesianProductBenchmark(Quantities); + benchmark::RunSpecifiedBenchmarks(); +} Index: libcxx/benchmarks/algorithms.partition_point.bench.cpp =================================================================== --- libcxx/benchmarks/algorithms.partition_point.bench.cpp +++ libcxx/benchmarks/algorithms.partition_point.bench.cpp @@ -1,3 +1,11 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + #include #include #include