Browse Source

feat: codec convert benchmark

legacy
Dnomd343 2 years ago
parent
commit
025b79749d
  1. 74
      src/klotski_core/benchmark/benchmark.cc
  2. 10
      src/klotski_core/benchmark/benchmark.h
  3. 8
      src/klotski_core/benchmark/chore.cc
  4. 21
      src/klotski_core/ffi/tmain.cc
  5. 2
      src/klotski_core/short_code/convert.cc

74
src/klotski_core/benchmark/benchmark.cc

@ -65,3 +65,77 @@ double Benchmark::common_code_from_string(TIME format) noexcept {
} }
return time_format(start, format) / (double)all_common_codes_str.size(); return time_format(start, format) / (double)all_common_codes_str.size();
} }
double Benchmark::common_code_to_raw_code(TIME format) noexcept {
if (!data_ready) {
return -1; // data no ready -> skip
}
auto start = clock();
for (auto &&common_code : all_common_codes) {
RawCode{std::forward<CommonCode>(common_code)};
}
return time_format(start, format) / (double)all_common_codes.size();
}
double Benchmark::raw_code_to_common_code(TIME format) noexcept {
if (!data_ready) {
return -1; // data no ready -> skip
}
auto start = clock();
for (auto &&raw_code : all_raw_codes) {
raw_code.to_common_code();
}
return time_format(start, format) / (double)all_raw_codes.size();
}
double Benchmark::common_code_to_short_code(TIME format) noexcept {
if (!data_ready) {
return -1; // data no ready -> skip
}
uint32_t num = 0;
ShortCode::speed_up(ShortCode::NORMAL);
auto start = clock();
for (uint64_t i = 0; i < all_common_codes.size(); i += 1000) {
ShortCode{all_common_codes[i]};
++num;
}
return time_format(start, format) / (double)num;
}
double Benchmark::short_code_to_common_code(TIME format) noexcept {
if (!data_ready) {
return -1; // data no ready -> skip
}
uint32_t num = 0;
ShortCode::speed_up(ShortCode::NORMAL);
auto start = clock();
for (uint64_t i = 0; i < all_short_codes.size(); i += 1000) {
all_short_codes[i].to_common_code();
++num;
}
return time_format(start, format) / (double)num;
}
double Benchmark::common_code_to_short_code_fast(TIME format) noexcept {
if (!data_ready) {
return -1; // data no ready -> skip
}
ShortCode::speed_up(ShortCode::FAST);
auto start = clock();
for (auto &&common_code : all_common_codes) {
ShortCode{std::forward<CommonCode>(common_code)};
}
return time_format(start, format) / (double)all_common_codes.size();
}
double Benchmark::short_code_to_common_code_fast(TIME format) noexcept {
if (!data_ready) {
return -1; // data no ready -> skip
}
ShortCode::speed_up(ShortCode::FAST);
auto start = clock();
for (auto &&short_code : all_short_codes) {
short_code.to_common_code();
}
return time_format(start, format) / (double)all_short_codes.size();
}

10
src/klotski_core/benchmark/benchmark.h

@ -43,6 +43,16 @@ namespace klotski {
static double common_code_to_string(TIME format = NS) noexcept; static double common_code_to_string(TIME format = NS) noexcept;
static double common_code_from_string(TIME format = NS) noexcept; static double common_code_from_string(TIME format = NS) noexcept;
static double common_code_to_raw_code(TIME format = NS) noexcept;
static double raw_code_to_common_code(TIME format = NS) noexcept;
static double common_code_to_short_code(TIME format = NS) noexcept;
static double short_code_to_common_code(TIME format = NS) noexcept;
static double common_code_to_short_code_fast(TIME format = NS) noexcept;
static double short_code_to_common_code_fast(TIME format = NS) noexcept;
// static float codec_common_to_raw(TIME format = US); // static float codec_common_to_raw(TIME format = US);
// static float codec_raw_to_common(TIME format = US); // static float codec_raw_to_common(TIME format = US);
// static float codec_common_to_short(); // static float codec_common_to_short();

8
src/klotski_core/benchmark/chore.cc

@ -49,13 +49,15 @@ void Benchmark::data_preparation() noexcept {
std::iota(tmp.begin(), tmp.end(), 0); std::iota(tmp.begin(), tmp.end(), 0);
all_short_codes.reserve(klotski::SHORT_CODE_LIMIT); all_short_codes.reserve(klotski::SHORT_CODE_LIMIT);
for (auto &&short_code : tmp) { for (auto &&short_code : tmp) { // 0 ~ (SHORT_CODE_LIMIT - 1)
all_short_codes.emplace_back(ShortCode::unsafe_create(short_code)); all_short_codes.emplace_back(ShortCode::unsafe_create(short_code));
} }
all_short_codes_str.reserve(klotski::SHORT_CODE_LIMIT); all_short_codes_str.reserve(klotski::SHORT_CODE_LIMIT);
for (auto &&short_code : all_short_codes) { for (auto &&short_code : tmp) { // 0 ~ (SHORT_CODE_LIMIT - 1)
all_short_codes_str.emplace_back(short_code.to_string()); all_short_codes_str.emplace_back(
ShortCode::unsafe_create(short_code).to_string()
);
} }
/// common code info preparation /// common code info preparation

21
src/klotski_core/ffi/tmain.cc

@ -23,11 +23,22 @@ void tmain() {
Benchmark::data_preparation(); Benchmark::data_preparation();
std::cout << Benchmark::short_code_to_string() << "ns" << std::endl; std::cout << "start benchmark" << std::endl;
std::cout << Benchmark::short_code_from_string() << "ns" << std::endl;
// std::cout << Benchmark::short_code_to_string() << "ns" << std::endl;
std::cout << Benchmark::common_code_to_string() << "ns" << std::endl; // std::cout << Benchmark::short_code_from_string() << "ns" << std::endl;
std::cout << Benchmark::common_code_from_string() << "ns" << std::endl; //
// std::cout << Benchmark::common_code_to_string() << "ns" << std::endl;
// std::cout << Benchmark::common_code_from_string() << "ns" << std::endl;
//
// std::cout << Benchmark::common_code_to_raw_code() << "ns" << std::endl;
// std::cout << Benchmark::raw_code_to_common_code() << "ns" << std::endl;
std::cout << Benchmark::common_code_to_short_code() << "ns" << std::endl;
std::cout << Benchmark::short_code_to_common_code() << "ns" << std::endl;
std::cout << Benchmark::common_code_to_short_code_fast() << "ns" << std::endl;
std::cout << Benchmark::short_code_to_common_code_fast() << "ns" << std::endl;
return; return;

2
src/klotski_core/short_code/convert.cc

@ -70,7 +70,7 @@ uint32_t ShortCode::fast_encode(uint64_t common_code) noexcept { // common code
/// NOTE: ensure that input short code is valid! /// NOTE: ensure that input short code is valid!
uint64_t ShortCode::fast_decode(uint32_t short_code) noexcept { // short code --> common code uint64_t ShortCode::fast_decode(uint32_t short_code) noexcept { // short code --> common code
auto offset = std::upper_bound( // using binary search auto offset = std::upper_bound( // using binary search
ALL_CASES_OFFSET, ALL_CASES_OFFSET + 16, short_code ALL_CASES_OFFSET, ALL_CASES_OFFSET + 16, short_code
) - 1; ) - 1;
uint64_t head = offset - ALL_CASES_OFFSET; // head index uint64_t head = offset - ALL_CASES_OFFSET; // head index
return (head << 32) | AllCases::fetch()[head][short_code - *offset]; // release common code return (head << 32) | AllCases::fetch()[head][short_code - *offset]; // release common code

Loading…
Cancel
Save