#include #include #include #include #include #include "benchmark/benchmark.h" #include "glog/logging.h" #include "logger.hh" //! AI generated static double taylor_approximation( double x, // Point to evaluate double a, // Expansion point int n, // Number of terms const std::function& func, // f(a) const std::function& derivative // f^(k)(a) ) { double result = 0.0; double power = 1.0; // (x - a)^0 double factorial = 1.0; // 0! // Zeroth derivative term (k=0) result += func(a) * power / factorial; // Subsequent terms (k=1 to n-1) for (int k = 1; k < n; k++) { factorial *= k; // k! power *= (x - a); // (x - a)^k double deriv = derivative(k, a); result += deriv * power / factorial; } return result; } static double calc_exp_taylor() { auto exp_deriv = [](int k, double a) -> double { return std::exp(a); // k-th derivative of e^x is e^x }; double x = 1.0; double a_exp = 0.0; int terms = 10; return taylor_approximation( x, a_exp, terms, [](double a) { return std::exp(a); }, // f(a) exp_deriv); } vptyp::Logger& getLogger() { static vptyp::Logger l(std::cout); if (!l.isConfigured()) { l.configure({"size", "apprx", "garbage", "garbage2"}); } return l; } void initGlog() { if (!google::IsGoogleLoggingInitialized()) { google::InitGoogleLogging("loggerBench"); } } static void BM_taylor_logger(benchmark::State& state) { auto& l = getLogger(); for (auto _ : state) { double res = calc_exp_taylor(); l.add("apprx", res); } } static void BM_wo_logger(benchmark::State& state) { for (auto _ : state) double res = calc_exp_taylor(); } static void BM_taylor_glog(benchmark::State& state) { initGlog(); for (auto _ : state) { double res = calc_exp_taylor(); LOG(INFO) << "apprx=" << res; } } std::atomic& getValue() { static std::atomic d; return d; } static void BM_taylor_atomic_upd(benchmark::State& state) { for (auto _ : state) { double res = calc_exp_taylor(); getValue().store(res); } } void updMapValue(double res, const std::string& key) { static std::unordered_map d = {{"key", 10.0}, {"assembly", 11.0}, {"draw", 123.3}, {"d2", 0.0}, {"d55", 0.23}}; d[key] = res; } static void BM_taylor_map_upd(benchmark::State& state) { for (auto _ : state) { double res = calc_exp_taylor(); updMapValue(res, "assembly"); } } void updValue(double res) { static double d; static std::mutex mtx; { std::lock_guard lock(mtx); d = res; } } static void BM_taylor_mutex_upd(benchmark::State& state) { for (auto _ : state) { double res = calc_exp_taylor(); updValue(res); } } BENCHMARK(BM_wo_logger); BENCHMARK(BM_taylor_logger); BENCHMARK(BM_taylor_glog); BENCHMARK(BM_taylor_atomic_upd); BENCHMARK(BM_taylor_mutex_upd); BENCHMARK(BM_taylor_map_upd); std::string caesar_encoder(const std::string& input) { static constexpr int small = 'a'; static constexpr int big = 'A'; std::string encoded; encoded.reserve(input.size()); for (auto& c : input) { assert((c - small < 25 && c - small >= 0) || (c - big < 25 && c - big >= 0)); int id, begin; if (c - small < 0) { id = c - big; begin = big; } else { id = c - small; begin = small; } encoded += begin + (id + 3) % 25; } return encoded; } int caesar_base(int size = 1000) { std::string a(size, 'a'); auto res = caesar_encoder(a); return size; } static void BM_caesar_wo_logger(benchmark::State& state) { for (auto _ : state) { int res = caesar_base(); } } static void BM_caesar_logger(benchmark::State& state) { auto& l = getLogger(); for (auto _ : state) { int res = caesar_base(); l.add("size", res); } } BENCHMARK(BM_caesar_logger); BENCHMARK(BM_caesar_wo_logger); static void DoNothing(benchmark::State& state) { while (state.KeepRunning()) ; } BENCHMARK(DoNothing); BENCHMARK_MAIN();