pipeline/tests/pipeline.test.cpp
2025-08-06 21:16:22 +01:00

79 lines
2.0 KiB
C++

#include <benchmark/benchmark.h>
#include <freepipe/freepipe.hpp>
#include <pipeline/pipeline.hpp>
#include <pipes/pipes.hpp>
#include <tuple>
#include <vector>
namespace {
void BM_Pipeline(benchmark::State& state) {
using namespace pipeline;
for (auto _ : state) {
auto add = fn([](auto a, auto b) { return a + b; });
auto double_it = fn([](auto a) { return a * 2; });
auto square_it = fn([](auto a) { return a * a; });
auto pipeline = add | double_it | square_it;
auto ans = pipeline(0, 1);
benchmark::DoNotOptimize(ans);
}
}
BENCHMARK(BM_Pipeline);
void BM_Pipes(benchmark::State& state) {
using namespace pipes;
for (auto _ : state) {
std::vector<int> ans;
mux(std::vector<int>{0}, std::vector<int>{1}) >>=
transform([](int a, int b) { return a + b; }) >>=
transform([](int a) { return a * 2; }) >>=
transform([](int a) { return a * a; }) >>= push_back(ans);
benchmark::DoNotOptimize(ans);
}
}
BENCHMARK(BM_Pipes);
void BM_Pipe(benchmark::State &state) {
using namespace freepipe;
for (auto _ : state) {
const Pipe p;
auto r = p | [] { return std::tuple{0, 1}; } | [](auto ab) {
auto a = std::get<0>(ab);
auto b = std::get<1>(ab);
return a + b;
} | [](auto a) {
return a * 2;
} | [](auto a) { return a * a; };
benchmark::DoNotOptimize(r);
}
}
BENCHMARK(BM_Pipe);
} // namespace
// 2025-08-06T21:13:50+01:00
// Running /home/murta/src/pipeline/build/tests/pipeline.test
// Run on (16 X 3199.99 MHz CPU s)
// CPU Caches:
// L1 Data 32 KiB (x8)
// L1 Instruction 64 KiB (x8)
// L2 Unified 512 KiB (x8)
// L3 Unified 8192 KiB (x2)
// Load Average: 1.68, 1.49, 0.97
// -------------------------------------------------------
// Benchmark Time CPU Iterations
// -------------------------------------------------------
// BM_Pipeline 0.323 ns 0.323 ns 2163778209
// BM_Pipes 12.2 ns 12.2 ns 57225038
// BM_Pipe 0.320 ns 0.320 ns 2173384108