From 6752420d10099bfa74c76e72ef2b2d816bfbeb52 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 18 May 2024 16:15:48 +0800 Subject: [PATCH] feat: core benchmark framework --- src/core/CMakeLists.txt | 4 ++ src/core/benchmark/codec.cc | 85 +++++++++++++++++++++++++++++++++++++ src/core/main.cc | 11 +++-- 3 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 src/core/benchmark/codec.cc diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 689c246..f9ea8e3 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -33,3 +33,7 @@ add_library(klotski::core ALIAS klotski_core) # TODO: just for dev testing add_executable(klotski_core_bin main.cc) target_link_libraries(klotski_core_bin PRIVATE klotski_core) + +add_executable(codec_benchmark benchmark/codec.cc) +target_compile_options(codec_benchmark PRIVATE -fno-rtti -fno-exceptions) +target_link_libraries(codec_benchmark PRIVATE klotski::core benchmark::benchmark_main) diff --git a/src/core/benchmark/codec.cc b/src/core/benchmark/codec.cc new file mode 100644 index 0000000..c2be8b4 --- /dev/null +++ b/src/core/benchmark/codec.cc @@ -0,0 +1,85 @@ +#include + +#include "benchmark/benchmark.h" + +#include "all_cases/all_cases.h" + +#define private public +#include + +#include "common_code/common_code.h" + +using klotski::cases::AllCases; + +static std::vector all_common_codes() { + std::vector all_codes; + + for (uint64_t head = 0; head < 16; ++head) { + for (const auto range : AllCases::instance().fetch()[head]) { + all_codes.emplace_back(head << 32 | range); + } + } + + return all_codes; +} + +static std::vector select_codes() { + auto codes = all_common_codes(); + + return {codes[343], codes[666], codes[114514], codes[35324], codes[123454], codes[76453], codes[93411]}; +} + +std::vector samples { + 0x00000303F, + 0x0000071F0, + 0x003339C40, + 0x000804340, + 0x0034C6D00, + 0x00230E0F0, + 0x002F371C0, +}; + +static void Demo(benchmark::State &state) { + + // auto samples = select_codes(); + // + // for (auto code : samples) { + // auto c = klotski::codec::CommonCode::create(code).value(); + // std::cout << c << std::endl; + // } + // std::cout << std::endl; + + // samples.clear(); + // samples = select_codes(); + // samples.emplace_back(0x00000303F); + // samples.emplace_back(0x0000071F0); + // samples.emplace_back(0x003339C40); + // samples.emplace_back(0x000804340); + // samples.emplace_back(0x0034C6D00); + // samples.emplace_back(0x00230E0F0); + // samples.emplace_back(0x002F371C0); + + for (auto _ : state) { + + for (auto code : samples) { + // auto volatile holder = klotski::codec::CommonCode::string_encode(code); + // auto volatile holder_1 = code + 213; + // auto volatile holder_2 = code + 123; + // auto volatile holder_3 = code + 233; + // auto volatile holder_4 = code + 412; + // auto volatile holder_5 = code + 896; + // auto volatile holder_6 = code + 154; + // auto volatile holder_7 = code + 124; + + auto ret = klotski::codec::CommonCode::string_encode(code); + // auto volatile str_1 = ret[0]; + // auto volatile str_2 = ret[1]; + // auto volatile str_3 = ret[2]; + } + + } +} + +BENCHMARK(Demo); + +BENCHMARK_MAIN(); diff --git a/src/core/main.cc b/src/core/main.cc index 3b126eb..915013c 100644 --- a/src/core/main.cc +++ b/src/core/main.cc @@ -1,5 +1,6 @@ #include #include +#include #include "core/core.h" #include "group/group.h" @@ -24,10 +25,12 @@ using klotski::codec::SHORT_CODE_LIMIT; int main() { const auto start = clock(); - auto kk = GroupUnion::create(123).value(); - std::cout << kk.size() << std::endl; - std::cout << kk.group_num() << std::endl; - std::cout << kk.max_group_size() << std::endl; + std::cout << std::format("{:09X}", 0x1A9BF0C00) << std::endl; + + // auto kk = GroupUnion::create(123).value(); + // std::cout << kk.size() << std::endl; + // std::cout << kk.group_num() << std::endl; + // std::cout << kk.max_group_size() << std::endl; // auto core = Core([](const uint64_t code, uint64_t) { // std::cout << RawCode::unsafe_create(code);