From 882b058df962c59a8eaf0f0185af3c8545e745d5 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Tue, 17 Jan 2023 23:55:46 +0800 Subject: [PATCH] test: benchmark for diff AllCases build methods --- src/all_cases/all_cases.cc | 3 +++ src/all_cases/all_cases.h | 8 ++++-- src/all_cases/basic_ranges.h | 5 +++- src/main.cc | 52 +++++++++++++++++++++++++++++------- src/utils/common.cc | 33 +++++++++++++++++++++++ src/utils/common.h | 4 ++- 6 files changed, 91 insertions(+), 14 deletions(-) diff --git a/src/all_cases/all_cases.cc b/src/all_cases/all_cases.cc index d5fa6d1..3365fb9 100644 --- a/src/all_cases/all_cases.cc +++ b/src/all_cases/all_cases.cc @@ -55,9 +55,12 @@ void AllCases::build_data() { // find all cases while (Common::range_reverse(basic_ranges[++index]) < next_min); // located next range --index; } else { +// if (!Common::check_range(head, basic_ranges[index])) { +// if (Common::check_case(head, basic_ranges[index])) { AllCases::data[head].emplace_back( Common::range_reverse(basic_ranges[index]) // release valid cases ); +// } } } } diff --git a/src/all_cases/all_cases.h b/src/all_cases/all_cases.h index 3884e9b..72a6858 100644 --- a/src/all_cases/all_cases.h +++ b/src/all_cases/all_cases.h @@ -18,10 +18,14 @@ public: static enum Status status(); static const std::vector (&fetch())[16]; -private: + // TODO: remove after test static bool available; - static std::mutex building; static std::vector data[16]; +private: +// static bool available; + static std::mutex building; +// static std::vector data[16]; + static void build_data(); }; diff --git a/src/all_cases/basic_ranges.h b/src/all_cases/basic_ranges.h index 4fb3378..c4089a6 100644 --- a/src/all_cases/basic_ranges.h +++ b/src/all_cases/basic_ranges.h @@ -17,6 +17,9 @@ public: static enum Status status(); static const std::vector& fetch(); + // TODO: remove after test + static std::vector data; + private: struct generate_t { int n1; // number of `00` @@ -27,7 +30,7 @@ private: static bool available; static std::mutex building; - static std::vector data; +// static std::vector data; static void build_data(); static void generate(generate_t info); diff --git a/src/main.cc b/src/main.cc index 0f87ee2..aceb048 100644 --- a/src/main.cc +++ b/src/main.cc @@ -94,7 +94,7 @@ int main() { // ShortCode::speed_up(ShortCode::NORMAL); // std::cout << "start verify" << std::endl; // - auto start_time = clock(); +// auto start_time = clock(); // // std::thread tasks[16]; // @@ -436,18 +436,50 @@ int main() { used_ranges.emplace(Common::range_reverse(range)); } } -// std::cout << used_ranges.size() << std::endl; +// std::cout << "used number: " << used_ranges.size() << std::endl; - for (const auto &range : BasicRanges::fetch()) { - if (used_ranges.find(range) == used_ranges.end()) { - printf("%08X -> no\n", Common::range_reverse(range)); // never used - } else { - printf("%08X -> yes\n", Common::range_reverse(range)); - } +// for (const auto &range : BasicRanges::fetch()) { +// if (used_ranges.find(range) == used_ranges.end()) { +// printf("%08X -> no\n", Common::range_reverse(range)); // never used +// } else { +// printf("%08X -> yes\n", Common::range_reverse(range)); +// } +// } + +// BasicRanges::build(); + + BasicRanges::data.clear(); + for (const auto &range : used_ranges) { + BasicRanges::data.emplace_back(Common::range_reverse(range)); + } + std::sort(BasicRanges::data.begin(), BasicRanges::data.end()); + for (auto &range : BasicRanges::data) { + range = Common::range_reverse(range); + } +// std::cout << "basic ranges size: " << BasicRanges::data.size() << std::endl; + + for (auto &a : AllCases::data) { + a.clear(); // clear built data } + AllCases::available = false; + + std::cout << "wait 3s" << std::endl; + sleep(3); + + std::cout << "start benchmark" << std::endl; + auto start_time = clock(); + + // rebuild all cases + AllCases::build(); + +// uint32_t sum = 0; +// for (const auto &a : AllCases::fetch()) { +// sum += a.size(); +// } +// std::cout << "sum = " << sum << std::endl; - std::cerr << (clock() - start_time) / CLOCKS_PER_SEC << "s" << std::endl; -// std::cerr << (clock() - start_time) * 1000 / CLOCKS_PER_SEC << "ms" << std::endl; +// std::cerr << (clock() - start_time) / CLOCKS_PER_SEC << "s" << std::endl; + std::cerr << (clock() - start_time) * 1000 / CLOCKS_PER_SEC << "ms" << std::endl; // std::cerr << (clock() - start_time) * 1000000 / CLOCKS_PER_SEC << "us" << std::endl; // std::cout << "complete benchmark" << std::endl; diff --git a/src/utils/common.cc b/src/utils/common.cc index 1886b76..5018aff 100644 --- a/src/utils/common.cc +++ b/src/utils/common.cc @@ -45,3 +45,36 @@ uint8_t Common::check_range(uint32_t head, uint32_t range) { // check generated } return 0; // pass check } + + +// TODO: remove after benchmark +bool Common::check_case(uint32_t head, uint32_t range) { // whether the head and range is valid + uint32_t mask = 0b110011 << head; // fill 2x2 block + for (int addr = 0; range; range >>= 2) { // traverse every 2-bits + while (mask >> addr & 0b1) { + ++addr; // search next not filled block + } + switch (range & 0b11) { + case 0b00: // space block + case 0b11: // 1x1 block + if (addr > 19) { // invalid address + return false; + } + mask |= 0b1 << addr; // fill 1x1 block + break; + case 0b10: // 2x1 block + if (addr > 15 || mask >> (addr + 4) & 0b1) { // invalid address + return false; + } + mask |= 0b10001 << addr; // fill 2x1 block + break; + case 0b01: // 1x2 block + if (addr > 18 || (addr & 0b11) == 0b11 || mask >> (addr + 1) & 0b1) { // invalid address + return false; + } + mask |= 0b11 << addr; // fill 1x2 block + break; + } + } + return true; // valid case +} diff --git a/src/utils/common.h b/src/utils/common.h index da713c2..136fd29 100644 --- a/src/utils/common.h +++ b/src/utils/common.h @@ -42,7 +42,9 @@ class Common { public: static uint32_t range_reverse(uint32_t bin); -// static bool check_case(uint32_t head, uint32_t range); + + // TODO: remove this function + static bool check_case(uint32_t head, uint32_t range); // TODO: with broken block offset return static uint8_t check_range(uint32_t head, uint32_t range);