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 "benchmark.h" |
||||
#include "all_cases.h" |
#include "all_cases.h" |
||||
|
|
||||
const char line[] = "------------------------"; |
using namespace klotski; |
||||
|
|
||||
void Benchmark::basic_ranges(std::ostream &out) { |
float Benchmark::basic_ranges(Benchmark::TIME format) { |
||||
out << line << std::endl; |
|
||||
out << "Basic Ranges Benchmark" << std::endl; |
|
||||
if (BasicRanges::status() != BasicRanges::NO_INIT) { |
if (BasicRanges::status() != BasicRanges::NO_INIT) { |
||||
out << "already built -> " << color_red("skip") << std::endl; |
return -1; // data already built -> skip
|
||||
out << line << std::endl; |
|
||||
return; |
|
||||
} |
} |
||||
/// start benchmark process
|
|
||||
auto start = clock(); |
auto start = clock(); |
||||
BasicRanges::build(); |
BasicRanges::build(); |
||||
out << "time -> " << time_ms(start) << std::endl; |
return time_format(start, format); |
||||
out << line << std::endl; |
|
||||
} |
} |
||||
|
|
||||
void Benchmark::all_cases(std::ostream &out) { |
float Benchmark::all_cases(Benchmark::TIME format) { |
||||
out << line << std::endl; |
|
||||
out << "All Cases Benchmark" << std::endl; |
|
||||
if (AllCases::status() != AllCases::NO_INIT) { |
if (AllCases::status() != AllCases::NO_INIT) { |
||||
out << "already built -> " << color_red("skip") << std::endl; |
return -1; // data already built -> skip
|
||||
out << line << std::endl; |
|
||||
return; |
|
||||
} |
} |
||||
/// preparing benchmark data
|
BasicRanges::build(); // preparing benchmark data
|
||||
BasicRanges::build(); |
|
||||
/// start benchmark process
|
|
||||
auto start = clock(); |
auto start = clock(); |
||||
AllCases::build(); |
AllCases::build(); |
||||
out << "time -> " << time_ms(start) << std::endl; |
return time_format(start, format); |
||||
out << line << std::endl; |
|
||||
} |
} |
||||
|
@ -1,45 +1,66 @@ |
|||||
#include "benchmark.h" |
#include "benchmark.h" |
||||
|
|
||||
|
using namespace klotski; |
||||
|
|
||||
// TODO: remove std::cout output -> only return benchmark status
|
// TODO: remove std::cout output -> only return benchmark status
|
||||
|
|
||||
/// colorful string
|
/// colorful string
|
||||
std::string Benchmark::color_red(const std::string &str) { |
//std::string Benchmark::color_red(const std::string &str) {
|
||||
return std::string("\033[31m") + str + "\033[0m"; |
// return std::string("\033[31m") + str + "\033[0m";
|
||||
} |
//}
|
||||
|
//
|
||||
std::string Benchmark::color_blue(const std::string &str) { |
//std::string Benchmark::color_blue(const std::string &str) {
|
||||
return std::string("\033[36m") + str + "\033[0m"; |
// return std::string("\033[36m") + str + "\033[0m";
|
||||
} |
//}
|
||||
|
//
|
||||
std::string Benchmark::color_green(const std::string &str) { |
//std::string Benchmark::color_green(const std::string &str) {
|
||||
return std::string("\033[32m") + str + "\033[0m"; |
// return std::string("\033[32m") + str + "\033[0m";
|
||||
} |
//}
|
||||
|
//
|
||||
std::string Benchmark::color_yellow(const std::string &str) { |
//std::string Benchmark::color_yellow(const std::string &str) {
|
||||
return std::string("\033[33m") + str + "\033[0m"; |
// return std::string("\033[33m") + str + "\033[0m";
|
||||
} |
//}
|
||||
|
|
||||
/// used-time to green string
|
/// used-time to green string
|
||||
std::string Benchmark::time_s(clock_t start) { |
//std::string Benchmark::time_s(clock_t start) {
|
||||
return color_green( |
// return color_green(
|
||||
std::to_string((clock() - start) / CLOCKS_PER_SEC) + "s" |
// std::to_string((clock() - start) / CLOCKS_PER_SEC) + "s"
|
||||
); |
// );
|
||||
} |
//}
|
||||
|
//
|
||||
std::string Benchmark::time_ms(clock_t start) { |
//std::string Benchmark::time_ms(clock_t start) {
|
||||
return color_green( |
// return color_green(
|
||||
std::to_string((clock() - start) * 1000 / CLOCKS_PER_SEC) + "ms" |
// std::to_string((clock() - start) * 1000 / CLOCKS_PER_SEC) + "ms"
|
||||
); |
// );
|
||||
} |
//}
|
||||
|
//
|
||||
std::string Benchmark::time_us(clock_t start) { |
//std::string Benchmark::time_us(clock_t start) {
|
||||
return color_green( |
// return color_green(
|
||||
std::to_string((clock() - start) * 1000 * 1000 / CLOCKS_PER_SEC) + "us" |
// 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_ns(clock_t start) { |
float Benchmark::time_format(clock_t start, Benchmark::TIME format) { |
||||
return color_green( |
auto time = float(clock() - start); |
||||
std::to_string((clock() - start) * 1000 * 1000 * 1000 / CLOCKS_PER_SEC) + "us" |
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; |
||||
|
} |
||||
|
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 <cstdio> |
||||
#include <cstdint> |
#include <cstdint> |
||||
|
|
||||
|
#include <iostream> |
||||
|
|
||||
#include "klotski.h" |
#include "klotski.h" |
||||
|
//#include "core.h"
|
||||
|
|
||||
#include "common.h" |
#include "common.h" |
||||
//#include "core.h"
|
#include "benchmark.h" |
||||
|
|
||||
|
using namespace klotski; |
||||
|
|
||||
void tmain() { |
void tmain() { |
||||
printf("tmain start\n"); |
printf("tmain start\n"); |
||||
|
|
||||
uint64_t common_code = 0x1A9BC0C00; |
// uint64_t common_code = 0x1A9BC0C00;
|
||||
|
// klotski::Common::range_reverse(common_code);
|
||||
klotski::Common::range_reverse(common_code); |
|
||||
|
|
||||
// uint64_t raw_code = 0x0603EDF5CAFFF5E2;
|
|
||||
|
|
||||
// auto core = Core([](uint64_t code, uint64_t mask) {
|
|
||||
// return;
|
|
||||
// });
|
|
||||
|
|
||||
// for (uint32_t i = 0; i < 1000000; ++i) {
|
std::cout << Benchmark::basic_ranges(Benchmark::MS) << std::endl; |
||||
// core.next_cases(raw_code, 0);
|
std::cout << Benchmark::all_cases(Benchmark::MS) << std::endl; |
||||
// }
|
|
||||
|
|
||||
printf("tmain exit\n"); |
printf("tmain exit\n"); |
||||
} |
} |
||||
|
Loading…
Reference in new issue