From 0b1265128e230b82eb32cba60692fa8fbbc69edc Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 22 Jan 2023 14:54:42 +0800 Subject: [PATCH] feat: Benchmark module --- src/CMakeLists.txt | 6 ++++++ src/benchmark/CMakeLists.txt | 1 + src/benchmark/benchmark.cc | 39 ++++++++++++++++++++++++++++++++++++ src/benchmark/benchmark.h | 18 +++++++++++++++++ src/main.cc | 9 +++++++++ 5 files changed, 73 insertions(+) create mode 100644 src/benchmark/CMakeLists.txt create mode 100644 src/benchmark/benchmark.cc create mode 100644 src/benchmark/benchmark.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4c32ed7..712a08e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -15,6 +15,8 @@ include_directories(fast_cal) include_directories(graph) +include_directories(benchmark) + ################################################ add_subdirectory(utils) @@ -30,6 +32,8 @@ add_subdirectory(fast_cal) add_subdirectory(graph) +add_subdirectory(benchmark) + ################################################ add_executable(klotski main.cc) @@ -49,6 +53,8 @@ target_link_libraries(klotski fast_cal) target_link_libraries(klotski graph) +target_link_libraries(klotski benchmark) + target_link_libraries(klotski pthread) ################################################ diff --git a/src/benchmark/CMakeLists.txt b/src/benchmark/CMakeLists.txt new file mode 100644 index 0000000..3f5a7c1 --- /dev/null +++ b/src/benchmark/CMakeLists.txt @@ -0,0 +1 @@ +add_library(benchmark benchmark.cc) diff --git a/src/benchmark/benchmark.cc b/src/benchmark/benchmark.cc new file mode 100644 index 0000000..db27da4 --- /dev/null +++ b/src/benchmark/benchmark.cc @@ -0,0 +1,39 @@ +#include "benchmark.h" + +#include "basic_ranges.h" + +const char split_line[] = "------------------------"; + + + +void Benchmark::basic_ranges(std::ostream &out) { + out << split_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; + return; + } + 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"; +} + +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"; +} diff --git a/src/benchmark/benchmark.h b/src/benchmark/benchmark.h new file mode 100644 index 0000000..749e5fd --- /dev/null +++ b/src/benchmark/benchmark.h @@ -0,0 +1,18 @@ +#pragma once + +#include + +class Benchmark { +public: + + + 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); + +}; diff --git a/src/main.cc b/src/main.cc index 7b65bd4..a56faa9 100644 --- a/src/main.cc +++ b/src/main.cc @@ -10,6 +10,8 @@ #include "common.h" #include "graph.h" +#include "benchmark.h" + #include #include #include @@ -18,6 +20,13 @@ //int main(int argc, char *argv[]) { int main() { +// BasicRanges::build(); + + Benchmark::basic_ranges(std::cout); + + return 0; + + BasicRanges::build(); // AllCases::build();