From ff00962b30811f2e7b7b890e5269cf3e20c59f0d Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 1 Mar 2023 18:37:40 +0800 Subject: [PATCH] perf: update Benchmark module --- src/klotski_core/benchmark/benchmark.cc | 6 ++-- src/klotski_core/benchmark/benchmark.h | 26 ++++++++++---- src/klotski_core/benchmark/chore.cc | 48 ++----------------------- src/klotski_core/core/core.h | 2 +- src/klotski_core/ffi/tmain.cc | 35 ++++-------------- 5 files changed, 31 insertions(+), 86 deletions(-) diff --git a/src/klotski_core/benchmark/benchmark.cc b/src/klotski_core/benchmark/benchmark.cc index cecd9a1..570d042 100644 --- a/src/klotski_core/benchmark/benchmark.cc +++ b/src/klotski_core/benchmark/benchmark.cc @@ -1,9 +1,9 @@ #include "benchmark.h" #include "all_cases.h" -using namespace klotski; +using klotski::Benchmark; -float Benchmark::basic_ranges(Benchmark::TIME format) { +float Benchmark::basic_ranges(TIME format) { if (BasicRanges::status() != BasicRanges::NO_INIT) { return -1; // data already built -> skip } @@ -12,7 +12,7 @@ float Benchmark::basic_ranges(Benchmark::TIME format) { return time_format(start, format); } -float Benchmark::all_cases(Benchmark::TIME format) { +float Benchmark::all_cases(TIME format) { if (AllCases::status() != AllCases::NO_INIT) { return -1; // data already built -> skip } diff --git a/src/klotski_core/benchmark/benchmark.h b/src/klotski_core/benchmark/benchmark.h index 79315e2..c48bfb0 100644 --- a/src/klotski_core/benchmark/benchmark.h +++ b/src/klotski_core/benchmark/benchmark.h @@ -1,6 +1,22 @@ #pragma once -#include +/// This is a built-in performance testing module for klotski core, which will +/// directly call the underlying functions to avoid the additional overhead +/// of the glue ABI. + +/// Multiple test items are provided here, and the corresponding float value is +/// returned according to the specified time format (seconds, milliseconds, +/// microseconds, nanoseconds). + +/// The test items are all multi-thread safe, but you should not run multiple +/// test items at the same time, which will lead to unstable tests in many ways, +/// such as changes in CPU turbo frequency. + +/// Pay attention to the two test items `basic_ranges` and `all_cases`, they can +/// only be run once (the reason for the construction of static data), and cannot +/// be run after short code related items. + +#include namespace klotski { class Benchmark { @@ -8,14 +24,10 @@ namespace klotski { enum TIME { S, MS, US, NS }; - static float all_cases(enum TIME format); - static float basic_ranges(enum TIME format); + static float all_cases(TIME format = MS); + static float basic_ranges(TIME format = MS); private: -// static std::string time_s(clock_t start); -// static std::string time_ms(clock_t start); -// static std::string time_us(clock_t start); -// static std::string time_ns(clock_t start); static float time_format(clock_t start, enum TIME format); }; } diff --git a/src/klotski_core/benchmark/chore.cc b/src/klotski_core/benchmark/chore.cc index 43cf7ee..0d2bd1c 100644 --- a/src/klotski_core/benchmark/chore.cc +++ b/src/klotski_core/benchmark/chore.cc @@ -1,52 +1,8 @@ #include "benchmark.h" -using namespace klotski; +using klotski::Benchmark; -// TODO: remove std::cout output -> only return benchmark status - -/// colorful string -//std::string Benchmark::color_red(const std::string &str) { -// return std::string("\033[31m") + str + "\033[0m"; -//} -// -//std::string Benchmark::color_blue(const std::string &str) { -// return std::string("\033[36m") + str + "\033[0m"; -//} -// -//std::string Benchmark::color_green(const std::string &str) { -// return std::string("\033[32m") + str + "\033[0m"; -//} -// -//std::string Benchmark::color_yellow(const std::string &str) { -// return std::string("\033[33m") + str + "\033[0m"; -//} - -/// used-time to green string -//std::string Benchmark::time_s(clock_t start) { -// return color_green( -// std::to_string((clock() - start) / CLOCKS_PER_SEC) + "s" -// ); -//} -// -//std::string Benchmark::time_ms(clock_t start) { -// return color_green( -// std::to_string((clock() - start) * 1000 / CLOCKS_PER_SEC) + "ms" -// ); -//} -// -//std::string Benchmark::time_us(clock_t start) { -// return color_green( -// std::to_string((clock() - start) * 1000 * 1000 / CLOCKS_PER_SEC) + "us" -// ); -//} -// -//std::string Benchmark::time_ns(clock_t start) { -// return color_green( -// std::to_string((clock() - start) * 1000 * 1000 * 1000 / CLOCKS_PER_SEC) + "us" -// ); -//} - -float Benchmark::time_format(clock_t start, Benchmark::TIME format) { +float Benchmark::time_format(clock_t start, TIME format) { auto time = float(clock() - start); switch (format) { case S: diff --git a/src/klotski_core/core/core.h b/src/klotski_core/core/core.h index 4ebab3a..91a084b 100644 --- a/src/klotski_core/core/core.h +++ b/src/klotski_core/core/core.h @@ -24,7 +24,7 @@ /// the help of a sub-BFS search, the target can be obtained. It can be shown that this step /// produces at most 15 derived layouts, so it can be stored and computed in a queue of length /// 16. By performing such a search on each block in the layout, we can get all the next-step -/// layouts, which have a minimum of 0 and a maximum of 68. +/// layouts, which have a minimum of `0` and a maximum of `68`. /// For a single block, search for the situation after it has moved one grid, `addr` is its current /// position information, which is (0 ~ 19) * 3. When moving up and down, judge the value of `addr` diff --git a/src/klotski_core/ffi/tmain.cc b/src/klotski_core/ffi/tmain.cc index 004c9bb..e471b0d 100644 --- a/src/klotski_core/ffi/tmain.cc +++ b/src/klotski_core/ffi/tmain.cc @@ -5,45 +5,22 @@ #include "klotski.h" #include "core.h" -//#include "common.h" +#include "common.h" #include "benchmark.h" #include "all_cases.h" #include "common_code.h" -void tmain() { - printf("tmain start\n"); - -// klotski::RawCode::from_common_code(0x1A9BF0C00).is_horizontal_mirror(); -// auto ret = klotski::RawCode::from_common_code(0x1A9BF0C00).to_horizontal_mirror(); -// auto ret = klotski::RawCode::from_common_code(0x4FEA13400).to_horizontal_mirror(); +using klotski::Benchmark; -// std::cout << ret.to_common_code() << std::endl << ret << std::endl; - -// klotski::RawCode::from_common_code(0x1A9BF0C00).is_vertical_mirror(); -// auto ret = klotski::RawCode::from_common_code(0x1A9BF0C00).to_vertical_mirror(); -// -// std::cout << ret.to_common_code() << std::endl << ret << std::endl; +void tmain() { +// printf("tmain start\n"); -// std::cout << klotski::RawCode::from_common_code(0x4FEA13400) << std::endl; -// std::cout << klotski::RawCode::from_common_code(0x8346AFC00) << std::endl; -// std::cout << klotski::RawCode::from_common_code(0x1A9BF0C00) << std::endl; - std::cout << klotski::RawCode::from_common_code(0x6BFA47000) << std::endl; + std::cout << "basic ranges: " << Benchmark::basic_ranges() << "ms" << std::endl; + std::cout << "all cases: " << Benchmark::all_cases() << "ms" << std::endl; return; -// printf("%d\n", ALL_CASES_SIZE_SUM); -// std::cout << ALL_CASES_SIZE_SUM << std::endl; - -// uint64_t common_code = 0x1A9BC0C00; -// klotski::Common::range_reverse(common_code); - -// printf("%f\n", Benchmark::basic_ranges(Benchmark::MS)); -// printf("%f\n", Benchmark::all_cases(Benchmark::MS)); -// std::cout << Benchmark::basic_ranges(Benchmark::MS) << std::endl; -// std::cout << Benchmark::all_cases(Benchmark::MS) << std::endl; - - std::vector next; auto core = klotski::Core([&next](uint64_t code, uint64_t) {