From 41c114e0d3051ad2d088cabf2a34c11874beb96c Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 30 Nov 2024 10:48:14 +0800 Subject: [PATCH] feat: add benchmark of ranges --- src/core/CMakeLists.txt | 3 +- src/core/benchmark/ranges.cc | 66 ++++++++++++++++++++++++------ src/core/ranges/internal/derive.cc | 4 -- 3 files changed, 54 insertions(+), 19 deletions(-) delete mode 100644 src/core/ranges/internal/derive.cc diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index ed462bc..fdd95e0 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -1,12 +1,11 @@ cmake_minimum_required(VERSION 3.12) -project(klotski_core VERSION 0.0.1 LANGUAGES CXX) +project(klotski_core VERSION 0.0.2 LANGUAGES CXX) set(CMAKE_CXX_STANDARD 23) set(KLSK_CORE_SRC ranges/internal/spawn.cc ranges/internal/ranges.cc - ranges/internal/derive.cc all_cases/internal/basic_ranges.cc all_cases/internal/all_cases.cc diff --git a/src/core/benchmark/ranges.cc b/src/core/benchmark/ranges.cc index 01bb5f3..f10560a 100644 --- a/src/core/benchmark/ranges.cc +++ b/src/core/benchmark/ranges.cc @@ -4,33 +4,69 @@ #include "ranges/ranges.h" #include "all_cases/all_cases.h" +using klotski::cases::Ranges; using klotski::cases::AllCases; using klotski::cases::BasicRanges; +using klotski::cases::RangesUnion; -static void SpawnRanges(benchmark::State &state) { - // constexpr auto nums = target_nums(); +using klotski::group::BLOCK_NUM; +using klotski::cases::ALL_CASES_NUM; +using klotski::cases::BASIC_RANGES_NUM_; - for (auto _ : state) { +Ranges ranges_samples(const size_t num) { + auto &ranges = BasicRanges::instance().fetch(); + const size_t part_size = ranges.size() / num; + const size_t offset = part_size / 2; - klotski::cases::Ranges kk {}; - kk.reserve(7311921); + Ranges result; + for (size_t i = 0; i < num; ++i) { + result.emplace_back(ranges[i * part_size + offset]); + } + return result; +} - // for (uint32_t type_id = 0; type_id < klotski::cases::TYPE_ID_LIMIT; ++type_id) { - for (auto [n, n_2x1, n_1x1] : klotski::group::BLOCK_NUM) { - kk.spawn(n, n_2x1, n_1x1); +static void CheckRanges(benchmark::State &state) { + auto ranges = ranges_samples(state.range(0)); + ranges.reverse(); + for (auto _ : state) { + for (const auto range : ranges) { + for (const auto head : RangesUnion::Heads) { + benchmark::DoNotOptimize(Ranges::check(static_cast(head), range)); } - // } + } } - + state.SetItemsProcessed(state.iterations() * state.range(0) * 12); } -static void RangesReverse(benchmark::State &state) { +static void ReverseRanges(benchmark::State &state) { auto ranges = BasicRanges::instance().fetch(); for (auto _ : state) { ranges.reverse(); } } +static void SpawnRanges(benchmark::State &state) { + for (auto _ : state) { + Ranges ranges {}; + ranges.reserve(BASIC_RANGES_NUM_); + for (const auto [n, n_2x1, n_1x1] : BLOCK_NUM) { + ranges.spawn(n, n_2x1, n_1x1); + } + } +} + +static void DeriveRanges(benchmark::State &state) { + auto ranges = BasicRanges::instance().fetch(); + ranges.reverse(); + for (auto _ : state) { + RangesUnion cases; + for (const auto head : RangesUnion::Heads) { + cases.ranges(head).reserve(ALL_CASES_NUM[head]); + ranges.derive(static_cast(head), cases.ranges(head)); + } + } +} + static void RangesUnionExport(benchmark::State &state) { auto &all_cases = AllCases::instance().fetch(); for (auto _ : state) { @@ -77,14 +113,18 @@ static void RangesAt(benchmark::State &state) { } } +BENCHMARK(CheckRanges)->Unit(benchmark::kMicrosecond)->Range(16, 256); + +// BENCHMARK(ReverseRanges)->Unit(benchmark::kMillisecond); + // BENCHMARK(SpawnRanges)->Unit(benchmark::kMillisecond); -// BENCHMARK(RangesReverse)->Unit(benchmark::kMillisecond); +// BENCHMARK(DeriveRanges)->Unit(benchmark::kMillisecond); // BENCHMARK(RangesUnionExport)->Unit(benchmark::kMillisecond); // BENCHMARK(RangesSize); -BENCHMARK(RangesAt); +// BENCHMARK(RangesAt); BENCHMARK_MAIN(); diff --git a/src/core/ranges/internal/derive.cc b/src/core/ranges/internal/derive.cc deleted file mode 100644 index e960d24..0000000 --- a/src/core/ranges/internal/derive.cc +++ /dev/null @@ -1,4 +0,0 @@ -// #include "utils/utility.h" -// #include "ranges/ranges.h" -// -// using klotski::cases::Ranges;