mirror of https://github.com/dnomd343/klotski.git
Dnomd343
2 years ago
6 changed files with 101 additions and 92 deletions
@ -1,36 +1,23 @@ |
|||
#include "benchmark.h" |
|||
#include "all_cases.h" |
|||
|
|||
const char line[] = "------------------------"; |
|||
using namespace klotski; |
|||
|
|||
void Benchmark::basic_ranges(std::ostream &out) { |
|||
out << line << std::endl; |
|||
out << "Basic Ranges Benchmark" << std::endl; |
|||
float Benchmark::basic_ranges(Benchmark::TIME format) { |
|||
if (BasicRanges::status() != BasicRanges::NO_INIT) { |
|||
out << "already built -> " << color_red("skip") << std::endl; |
|||
out << line << std::endl; |
|||
return; |
|||
return -1; // data already built -> skip
|
|||
} |
|||
/// start benchmark process
|
|||
auto start = clock(); |
|||
BasicRanges::build(); |
|||
out << "time -> " << time_ms(start) << std::endl; |
|||
out << line << std::endl; |
|||
return time_format(start, format); |
|||
} |
|||
|
|||
void Benchmark::all_cases(std::ostream &out) { |
|||
out << line << std::endl; |
|||
out << "All Cases Benchmark" << std::endl; |
|||
float Benchmark::all_cases(Benchmark::TIME format) { |
|||
if (AllCases::status() != AllCases::NO_INIT) { |
|||
out << "already built -> " << color_red("skip") << std::endl; |
|||
out << line << std::endl; |
|||
return; |
|||
return -1; // data already built -> skip
|
|||
} |
|||
/// preparing benchmark data
|
|||
BasicRanges::build(); |
|||
/// start benchmark process
|
|||
BasicRanges::build(); // preparing benchmark data
|
|||
auto start = clock(); |
|||
AllCases::build(); |
|||
out << "time -> " << time_ms(start) << std::endl; |
|||
out << line << std::endl; |
|||
return time_format(start, format); |
|||
} |
|||
|
@ -1,45 +1,66 @@ |
|||
#include "benchmark.h" |
|||
|
|||
using namespace klotski; |
|||
|
|||
// 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"; |
|||
} |
|||
//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_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"
|
|||
// );
|
|||
//}
|
|||
|
|||
std::string Benchmark::time_ms(clock_t start) { |
|||
return color_green( |
|||
std::to_string((clock() - start) * 1000 / CLOCKS_PER_SEC) + "ms" |
|||
); |
|||
float Benchmark::time_format(clock_t start, Benchmark::TIME format) { |
|||
auto time = float(clock() - start); |
|||
switch (format) { |
|||
case S: |
|||
time *= float(1); |
|||
break; |
|||
case MS: |
|||
time *= float(1000); |
|||
break; |
|||
case US: |
|||
time *= float(1000 * 1000); |
|||
break; |
|||
case NS: |
|||
time *= float(1000 * 1000 * 1000); |
|||
break; |
|||
} |
|||
|
|||
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" |
|||
); |
|||
return time / CLOCKS_PER_SEC; |
|||
} |
|||
|
@ -1,30 +1,24 @@ |
|||
/// WARN: c-style lib should not using `iostream`
|
|||
/// using `printf` for screen output in test process
|
|||
|
|||
#include <cstdio> |
|||
#include <cstdint> |
|||
|
|||
#include <iostream> |
|||
|
|||
#include "klotski.h" |
|||
//#include "core.h"
|
|||
|
|||
#include "common.h" |
|||
//#include "core.h"
|
|||
#include "benchmark.h" |
|||
|
|||
using namespace klotski; |
|||
|
|||
void tmain() { |
|||
printf("tmain start\n"); |
|||
|
|||
uint64_t common_code = 0x1A9BC0C00; |
|||
|
|||
klotski::Common::range_reverse(common_code); |
|||
|
|||
// uint64_t raw_code = 0x0603EDF5CAFFF5E2;
|
|||
|
|||
// auto core = Core([](uint64_t code, uint64_t mask) {
|
|||
// return;
|
|||
// });
|
|||
// uint64_t common_code = 0x1A9BC0C00;
|
|||
// klotski::Common::range_reverse(common_code);
|
|||
|
|||
// for (uint32_t i = 0; i < 1000000; ++i) {
|
|||
// core.next_cases(raw_code, 0);
|
|||
// }
|
|||
std::cout << Benchmark::basic_ranges(Benchmark::MS) << std::endl; |
|||
std::cout << Benchmark::all_cases(Benchmark::MS) << std::endl; |
|||
|
|||
printf("tmain exit\n"); |
|||
} |
|||
|
Loading…
Reference in new issue