-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmarks.cpp
More file actions
84 lines (69 loc) · 2.09 KB
/
benchmarks.cpp
File metadata and controls
84 lines (69 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (c) 2025 Clément Metz
// Licensed under the MIT License. See LICENSE file for details.
#include "./NamedType/named_type.hpp"
#include <catch2/benchmark/catch_benchmark.hpp>
#include <catch2/catch_test_macros.hpp>
#include <chrono>
#include <random>
#include <ranges>
#include <stronger.hpp>
namespace stronger::tests::benchmarks
{
namespace
{
using NamedTypeDouble = fluent::NamedType<double, struct DoubleTag, fluent::Arithmetic>;
using StrongerDouble = strong_type<double, tag()>;
constexpr size_t Iterations = 10'000'000ULL;
template <typename T>
std::vector<T> get_random()
{
static std::random_device rd;
static std::mt19937 gen(rd());
static std::uniform_real_distribution dis(-1000.0, 1000.0);
std::vector<T> result(Iterations, T(0.0));
std::ranges::generate(result, [] { return T(dis(gen)); });
return result;
}
template <typename T>
std::tuple<std::vector<T>, std::vector<T>, std::vector<T>> get_benchmark_params()
{
auto a = get_random<T>();
return std::make_tuple(a, get_random<T>(), std::move(a));
}
void benchmark(const auto& a, const auto& b, auto& result)
{
for(auto&& [valueA, valueB, valueResult] : std::views::zip(a, b, result))
valueResult = valueA + valueB * (valueA - valueB) / (valueA + valueB);
}
} // namespace
TEST_CASE("Benchmark: NamedTypeDouble vs StrongerDouble Addition Performance", "[benchmark]")
{
SECTION("double Addition")
{
auto [a, b, result] = get_benchmark_params<double>();
BENCHMARK("double")
{
benchmark(a, b, result);
return result;
};
}
SECTION("NamedType Addition")
{
auto [a, b, result] = get_benchmark_params<NamedTypeDouble>();
BENCHMARK("NamedType")
{
benchmark(a, b, result);
return result;
};
}
SECTION("stronger-cpp Addition")
{
auto [a, b, result] = get_benchmark_params<StrongerDouble>();
BENCHMARK("stronger-cpp")
{
benchmark(a, b, result);
return result;
};
}
}
} // namespace stronger::tests::benchmarks