Browse Source

update: enhance Benchmark module

master
Dnomd343 1 year ago
parent
commit
9bcf4e6803
  1. 3
      src/all_cases/CMakeLists.txt
  2. 10
      src/all_cases/all_cases.cc
  3. 7
      src/all_cases/all_cases.h
  4. 9
      src/all_cases/basic_ranges.cc
  5. 2
      src/all_cases/basic_ranges.h
  6. 2
      src/benchmark/CMakeLists.txt
  7. 49
      src/benchmark/benchmark.cc
  8. 18
      src/benchmark/benchmark.h
  9. 43
      src/benchmark/chore.cc
  10. 5
      src/main.cc

3
src/all_cases/CMakeLists.txt

@ -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)

10
src/all_cases/all_cases.cc

@ -1,6 +1,14 @@
#include "common.h"
#include "all_cases.h"
const uint32_t ALL_CASES_SIZE[16] = {
2942906, 2260392, 2942906, 0,
2322050, 1876945, 2322050, 0,
2322050, 1876945, 2322050, 0,
2942906, 2260392, 2942906, 0,
};
/// static variable initialize
std::mutex AllCases::building;
bool AllCases::available = false;
std::vector<uint32_t> AllCases::data[];
@ -43,7 +51,7 @@ void AllCases::build_data() { // find all cases
}
/// head -> 0/1/2 / 4/5/6 / 8/9/10 / 12/13/14
data[head].reserve(ALL_CASES_SIZE[head]); // memory pre-allocated
/// head(4-bits) + basic ranges(32-bits) ==check==> release valid cases
/// head(4-bits) + basic ranges(32-bits) --check--> valid cases
for (uint32_t index = 0; index < basic_ranges.size(); ++index) {
auto broken_offset = Common::check_range(head, basic_ranges[index]);
if (broken_offset) { // case invalid

7
src/all_cases/all_cases.h

@ -5,13 +5,6 @@
#include <cstdint>
#include "basic_ranges.h"
const uint32_t ALL_CASES_SIZE[16] = {
2942906, 2260392, 2942906, 0,
2322050, 1876945, 2322050, 0,
2322050, 1876945, 2322050, 0,
2942906, 2260392, 2942906, 0,
};
class AllCases : public BasicRanges {
public:
static void build();

9
src/all_cases/basic_ranges.cc

@ -3,6 +3,9 @@
#include "common.h"
#include "basic_ranges.h"
const uint32_t BASIC_RANGES_SIZE = 7311921;
/// static variable initialize
std::mutex BasicRanges::building;
bool BasicRanges::available = false;
std::vector<uint32_t> BasicRanges::data;
@ -63,8 +66,8 @@ void BasicRanges::generate(generate_t info) { // generate specific basic ranges
constexpr auto MASK_10 = (uint32_t)0b10 << 30;
constexpr auto MASK_11 = (uint32_t)0b11 << 30;
/// n4 n3 n2 n1
/// 00000000 00000000 00000000 00000000 (32-bits)
/// nx: n4 n3 n2 n1
/// 00000000 00000000 00000000 00000000 (32-bits)
struct build_t {
uint32_t nx; // (n4, n3, n2, n1)
uint32_t prefix;
@ -78,7 +81,7 @@ void BasicRanges::generate(generate_t info) { // generate specific basic ranges
.offset = 0,
});
while (!cache.empty()) { // queue without elements -> work complete
while (!cache.empty()) { // queue without element -> work complete
auto current = cache.front();
if (!current.nx) { // both n1, n2, n3, n4 -> 0
BasicRanges::data.emplace_back(current.prefix); // release prefix as basic range

2
src/all_cases/basic_ranges.h

@ -4,8 +4,6 @@
#include <vector>
#include <cstdint>
const uint32_t BASIC_RANGES_SIZE = 7311921;
class BasicRanges {
public:
enum Status {

2
src/benchmark/CMakeLists.txt

@ -1 +1 @@
add_library(benchmark benchmark.cc)
add_library(benchmark chore.cc benchmark.cc)

49
src/benchmark/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;
}

18
src/benchmark/benchmark.h

@ -4,15 +4,17 @@
class Benchmark {
public:
static void all_cases(std::ostream &out);
static void basic_ranges(std::ostream &out);
static inline void time_ms(clock_t start, std::ostream &out);
static inline void time_us(clock_t start, std::ostream &out);
static inline long time_ms_(clock_t start);
static inline std::string time_ms_str(clock_t start);
private:
static std::string time_s(clock_t start);
static std::string time_ms(clock_t start);
static std::string time_us(clock_t start);
static std::string time_ns(clock_t start);
static std::string color_red(const std::string &str);
static std::string color_blue(const std::string &str);
static std::string color_green(const std::string &str);
static std::string color_yellow(const std::string &str);
};

43
src/benchmark/chore.cc

@ -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"
);
}

5
src/main.cc

@ -22,6 +22,11 @@ int main() {
// BasicRanges::build();
// Benchmark::basic_ranges(std::cout);
// std::cout << std::endl;
Benchmark::all_cases(std::cout);
std::cout << std::endl;
Benchmark::basic_ranges(std::cout);
return 0;

Loading…
Cancel
Save