MicroLogger/bench/main.cc
2025-06-22 18:11:55 +00:00

106 lines
2.6 KiB
C++

#include <cmath>
#include <functional>
#include "benchmark/benchmark.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<double(double)>& func, // f(a)
const std::function<double(int, double)>& 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);
}
static void BM_taylor_logger(benchmark::State& state) {
vptyp::Logger l;
l.configure({"size", "apprx", "garbage", "garbage2"});
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();
}
BENCHMARK(BM_wo_logger);
BENCHMARK(BM_taylor_logger);
std::string caesar_encoder(const std::string& input) {
static constexpr int small = 141;
static constexpr int big = 101;
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) {
vptyp::Logger l;
l.configure({"size", "apprx", "garbage", "garbage2"});
for (auto _ : state) {
int res = caesar_base();
l.add("size", res);
}
}
BENCHMARK(BM_caesar_wo_logger);
BENCHMARK(BM_caesar_logger);
BENCHMARK_MAIN();