mirror of https://github.com/dnomd343/klotski.git
Dnomd343
2 years ago
10 changed files with 98 additions and 50 deletions
@ -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) |
|||
|
@ -1 +1 @@ |
|||
add_library(benchmark benchmark.cc) |
|||
add_library(benchmark chore.cc 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; |
|||
} |
|||
|
@ -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" |
|||
); |
|||
} |
Loading…
Reference in new issue