From 9bcf4e68036c3d92a13576bc8bfaa10f722e04d1 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 22 Jan 2023 15:30:44 +0800 Subject: [PATCH] update: enhance Benchmark module --- src/all_cases/CMakeLists.txt | 3 +-- src/all_cases/all_cases.cc | 10 ++++++- src/all_cases/all_cases.h | 7 ----- src/all_cases/basic_ranges.cc | 9 ++++--- src/all_cases/basic_ranges.h | 2 -- src/benchmark/CMakeLists.txt | 2 +- src/benchmark/benchmark.cc | 49 ++++++++++++++++------------------- src/benchmark/benchmark.h | 18 +++++++------ src/benchmark/chore.cc | 43 ++++++++++++++++++++++++++++++ src/main.cc | 5 ++++ 10 files changed, 98 insertions(+), 50 deletions(-) create mode 100644 src/benchmark/chore.cc diff --git a/src/all_cases/CMakeLists.txt b/src/all_cases/CMakeLists.txt index 29a672f..b41a565 100644 --- a/src/all_cases/CMakeLists.txt +++ b/src/all_cases/CMakeLists.txt @@ -1,4 +1,3 @@ -cmake_minimum_required(VERSION 3.0) - add_library(all_cases all_cases.cc basic_ranges.cc) + target_link_libraries(all_cases utils) diff --git a/src/all_cases/all_cases.cc b/src/all_cases/all_cases.cc index ab9adf7..7f8e5b7 100644 --- a/src/all_cases/all_cases.cc +++ b/src/all_cases/all_cases.cc @@ -1,6 +1,14 @@ #include "common.h" #include "all_cases.h" +const uint32_t ALL_CASES_SIZE[16] = { + 2942906, 2260392, 2942906, 0, + 2322050, 1876945, 2322050, 0, + 2322050, 1876945, 2322050, 0, + 2942906, 2260392, 2942906, 0, +}; + +/// static variable initialize std::mutex AllCases::building; bool AllCases::available = false; std::vector AllCases::data[]; @@ -43,7 +51,7 @@ void AllCases::build_data() { // find all cases } /// head -> 0/1/2 / 4/5/6 / 8/9/10 / 12/13/14 data[head].reserve(ALL_CASES_SIZE[head]); // memory pre-allocated - /// head(4-bits) + basic ranges(32-bits) ==check==> release valid cases + /// head(4-bits) + basic ranges(32-bits) --check--> valid cases for (uint32_t index = 0; index < basic_ranges.size(); ++index) { auto broken_offset = Common::check_range(head, basic_ranges[index]); if (broken_offset) { // case invalid diff --git a/src/all_cases/all_cases.h b/src/all_cases/all_cases.h index 3884e9b..936a473 100644 --- a/src/all_cases/all_cases.h +++ b/src/all_cases/all_cases.h @@ -5,13 +5,6 @@ #include #include "basic_ranges.h" -const uint32_t ALL_CASES_SIZE[16] = { - 2942906, 2260392, 2942906, 0, - 2322050, 1876945, 2322050, 0, - 2322050, 1876945, 2322050, 0, - 2942906, 2260392, 2942906, 0, -}; - class AllCases : public BasicRanges { public: static void build(); diff --git a/src/all_cases/basic_ranges.cc b/src/all_cases/basic_ranges.cc index 9c2a0c5..eed55b1 100644 --- a/src/all_cases/basic_ranges.cc +++ b/src/all_cases/basic_ranges.cc @@ -3,6 +3,9 @@ #include "common.h" #include "basic_ranges.h" +const uint32_t BASIC_RANGES_SIZE = 7311921; + +/// static variable initialize std::mutex BasicRanges::building; bool BasicRanges::available = false; std::vector BasicRanges::data; @@ -63,8 +66,8 @@ void BasicRanges::generate(generate_t info) { // generate specific basic ranges constexpr auto MASK_10 = (uint32_t)0b10 << 30; constexpr auto MASK_11 = (uint32_t)0b11 << 30; - /// n4 n3 n2 n1 - /// 00000000 00000000 00000000 00000000 (32-bits) + /// nx: n4 n3 n2 n1 + /// 00000000 00000000 00000000 00000000 (32-bits) struct build_t { uint32_t nx; // (n4, n3, n2, n1) uint32_t prefix; @@ -78,7 +81,7 @@ void BasicRanges::generate(generate_t info) { // generate specific basic ranges .offset = 0, }); - while (!cache.empty()) { // queue without elements -> work complete + while (!cache.empty()) { // queue without element -> work complete auto current = cache.front(); if (!current.nx) { // both n1, n2, n3, n4 -> 0 BasicRanges::data.emplace_back(current.prefix); // release prefix as basic range diff --git a/src/all_cases/basic_ranges.h b/src/all_cases/basic_ranges.h index 4fb3378..2b86c89 100644 --- a/src/all_cases/basic_ranges.h +++ b/src/all_cases/basic_ranges.h @@ -4,8 +4,6 @@ #include #include -const uint32_t BASIC_RANGES_SIZE = 7311921; - class BasicRanges { public: enum Status { diff --git a/src/benchmark/CMakeLists.txt b/src/benchmark/CMakeLists.txt index 3f5a7c1..b668aa6 100644 --- a/src/benchmark/CMakeLists.txt +++ b/src/benchmark/CMakeLists.txt @@ -1 +1 @@ -add_library(benchmark benchmark.cc) +add_library(benchmark chore.cc benchmark.cc) diff --git a/src/benchmark/benchmark.cc b/src/benchmark/benchmark.cc index db27da4..1099bd4 100644 --- a/src/benchmark/benchmark.cc +++ b/src/benchmark/benchmark.cc @@ -1,39 +1,36 @@ #include "benchmark.h" +#include "all_cases.h" -#include "basic_ranges.h" - -const char split_line[] = "------------------------"; - - +const char line[] = "------------------------"; void Benchmark::basic_ranges(std::ostream &out) { - out << split_line << std::endl; + out << line << std::endl; out << "Basic Ranges Benchmark" << std::endl; if (BasicRanges::status() != BasicRanges::NO_INIT) { - out << "already built -> skip" << std::endl; - out << split_line << std::endl; + out << "already built -> " << color_red("skip") << std::endl; + out << line << std::endl; return; } + /// start benchmark process auto start = clock(); BasicRanges::build(); -// time_ms(start, out); -// out << time_ms_(start); - out << time_ms_str(start); - out << std::endl << split_line << std::endl; -} - -long Benchmark::time_ms_(clock_t start) { - return (clock() - start) * 1000 / CLOCKS_PER_SEC; -} - -void Benchmark::time_ms(clock_t start, std::ostream &out) { - out << (clock() - start) * 1000 / CLOCKS_PER_SEC << "ms"; + out << "time -> " << time_ms(start) << std::endl; + out << line << std::endl; } -void Benchmark::time_us(clock_t start, std::ostream &out) { - out << (clock() - start) * 1000000 / CLOCKS_PER_SEC << "us"; -} - -std::string Benchmark::time_ms_str(clock_t start) { - return std::string("\033[32m") + std::to_string((clock() - start) * 1000 / CLOCKS_PER_SEC) + "ms\033[0m"; +void Benchmark::all_cases(std::ostream &out) { + out << line << std::endl; + out << "All Cases Benchmark" << std::endl; + if (AllCases::status() != AllCases::NO_INIT) { + out << "already built -> " << color_red("skip") << std::endl; + out << line << std::endl; + return; + } + /// preparing benchmark data + BasicRanges::build(); + /// start benchmark process + auto start = clock(); + AllCases::build(); + out << "time -> " << time_ms(start) << std::endl; + out << line << std::endl; } diff --git a/src/benchmark/benchmark.h b/src/benchmark/benchmark.h index 749e5fd..5daa6b1 100644 --- a/src/benchmark/benchmark.h +++ b/src/benchmark/benchmark.h @@ -4,15 +4,17 @@ class Benchmark { public: - - + static void all_cases(std::ostream &out); static void basic_ranges(std::ostream &out); - static inline void time_ms(clock_t start, std::ostream &out); - static inline void time_us(clock_t start, std::ostream &out); - - static inline long time_ms_(clock_t start); - - static inline std::string time_ms_str(clock_t start); +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 std::string color_red(const std::string &str); + static std::string color_blue(const std::string &str); + static std::string color_green(const std::string &str); + static std::string color_yellow(const std::string &str); }; diff --git a/src/benchmark/chore.cc b/src/benchmark/chore.cc new file mode 100644 index 0000000..195d516 --- /dev/null +++ b/src/benchmark/chore.cc @@ -0,0 +1,43 @@ +#include "benchmark.h" + +/// 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" + ); +} diff --git a/src/main.cc b/src/main.cc index a56faa9..5290ba6 100644 --- a/src/main.cc +++ b/src/main.cc @@ -22,6 +22,11 @@ int main() { // BasicRanges::build(); +// Benchmark::basic_ranges(std::cout); +// std::cout << std::endl; + Benchmark::all_cases(std::cout); + + std::cout << std::endl; Benchmark::basic_ranges(std::cout); return 0;