Browse Source

test: benchmark for diff AllCases build methods

master
Dnomd343 2 years ago
parent
commit
882b058df9
  1. 3
      src/all_cases/all_cases.cc
  2. 8
      src/all_cases/all_cases.h
  3. 5
      src/all_cases/basic_ranges.h
  4. 52
      src/main.cc
  5. 33
      src/utils/common.cc
  6. 4
      src/utils/common.h

3
src/all_cases/all_cases.cc

@ -55,9 +55,12 @@ void AllCases::build_data() { // find all cases
while (Common::range_reverse(basic_ranges[++index]) < next_min); // located next range
--index;
} else {
// if (!Common::check_range(head, basic_ranges[index])) {
// if (Common::check_case(head, basic_ranges[index])) {
AllCases::data[head].emplace_back(
Common::range_reverse(basic_ranges[index]) // release valid cases
);
// }
}
}
}

8
src/all_cases/all_cases.h

@ -18,10 +18,14 @@ public:
static enum Status status();
static const std::vector<uint32_t> (&fetch())[16];
private:
// TODO: remove after test
static bool available;
static std::mutex building;
static std::vector<uint32_t> data[16];
private:
// static bool available;
static std::mutex building;
// static std::vector<uint32_t> data[16];
static void build_data();
};

5
src/all_cases/basic_ranges.h

@ -17,6 +17,9 @@ public:
static enum Status status();
static const std::vector<uint32_t>& fetch();
// TODO: remove after test
static std::vector<uint32_t> data;
private:
struct generate_t {
int n1; // number of `00`
@ -27,7 +30,7 @@ private:
static bool available;
static std::mutex building;
static std::vector<uint32_t> data;
// static std::vector<uint32_t> data;
static void build_data();
static void generate(generate_t info);

52
src/main.cc

@ -94,7 +94,7 @@ int main() {
// ShortCode::speed_up(ShortCode::NORMAL);
// std::cout << "start verify" << std::endl;
//
auto start_time = clock();
// auto start_time = clock();
//
// std::thread tasks[16];
//
@ -436,18 +436,50 @@ int main() {
used_ranges.emplace(Common::range_reverse(range));
}
}
// std::cout << used_ranges.size() << std::endl;
// std::cout << "used number: " << used_ranges.size() << std::endl;
for (const auto &range : BasicRanges::fetch()) {
if (used_ranges.find(range) == used_ranges.end()) {
printf("%08X -> no\n", Common::range_reverse(range)); // never used
} else {
printf("%08X -> yes\n", Common::range_reverse(range));
}
// for (const auto &range : BasicRanges::fetch()) {
// if (used_ranges.find(range) == used_ranges.end()) {
// printf("%08X -> no\n", Common::range_reverse(range)); // never used
// } else {
// printf("%08X -> yes\n", Common::range_reverse(range));
// }
// }
// BasicRanges::build();
BasicRanges::data.clear();
for (const auto &range : used_ranges) {
BasicRanges::data.emplace_back(Common::range_reverse(range));
}
std::sort(BasicRanges::data.begin(), BasicRanges::data.end());
for (auto &range : BasicRanges::data) {
range = Common::range_reverse(range);
}
// std::cout << "basic ranges size: " << BasicRanges::data.size() << std::endl;
for (auto &a : AllCases::data) {
a.clear(); // clear built data
}
AllCases::available = false;
std::cout << "wait 3s" << std::endl;
sleep(3);
std::cout << "start benchmark" << std::endl;
auto start_time = clock();
// rebuild all cases
AllCases::build();
// uint32_t sum = 0;
// for (const auto &a : AllCases::fetch()) {
// sum += a.size();
// }
// std::cout << "sum = " << sum << std::endl;
std::cerr << (clock() - start_time) / CLOCKS_PER_SEC << "s" << std::endl;
// std::cerr << (clock() - start_time) * 1000 / CLOCKS_PER_SEC << "ms" << std::endl;
// std::cerr << (clock() - start_time) / CLOCKS_PER_SEC << "s" << std::endl;
std::cerr << (clock() - start_time) * 1000 / CLOCKS_PER_SEC << "ms" << std::endl;
// std::cerr << (clock() - start_time) * 1000000 / CLOCKS_PER_SEC << "us" << std::endl;
// std::cout << "complete benchmark" << std::endl;

33
src/utils/common.cc

@ -45,3 +45,36 @@ uint8_t Common::check_range(uint32_t head, uint32_t range) { // check generated
}
return 0; // pass check
}
// TODO: remove after benchmark
bool Common::check_case(uint32_t head, uint32_t range) { // whether the head and range is valid
uint32_t mask = 0b110011 << head; // fill 2x2 block
for (int addr = 0; range; range >>= 2) { // traverse every 2-bits
while (mask >> addr & 0b1) {
++addr; // search next not filled block
}
switch (range & 0b11) {
case 0b00: // space block
case 0b11: // 1x1 block
if (addr > 19) { // invalid address
return false;
}
mask |= 0b1 << addr; // fill 1x1 block
break;
case 0b10: // 2x1 block
if (addr > 15 || mask >> (addr + 4) & 0b1) { // invalid address
return false;
}
mask |= 0b10001 << addr; // fill 2x1 block
break;
case 0b01: // 1x2 block
if (addr > 18 || (addr & 0b11) == 0b11 || mask >> (addr + 1) & 0b1) { // invalid address
return false;
}
mask |= 0b11 << addr; // fill 1x2 block
break;
}
}
return true; // valid case
}

4
src/utils/common.h

@ -42,7 +42,9 @@
class Common {
public:
static uint32_t range_reverse(uint32_t bin);
// static bool check_case(uint32_t head, uint32_t range);
// TODO: remove this function
static bool check_case(uint32_t head, uint32_t range);
// TODO: with broken block offset return
static uint8_t check_range(uint32_t head, uint32_t range);

Loading…
Cancel
Save