-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfibonacci_sequence_benchmark.cpp
88 lines (66 loc) · 2.24 KB
/
fibonacci_sequence_benchmark.cpp
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
85
86
87
88
// Copyright (c) Omar Boukli-Hacene. All rights reserved.
// Distributed under an MIT-style license that can be
// found in the LICENSE file.
// SPDX-License-Identifier: MIT
#include <cstdint>
#include <catch2/catch_test_macros.hpp>
#include <nanobench.h>
#include <nameof.hpp>
#include "forfun/common.hpp"
#include "forfun/fibonacci_sequence.hpp"
inline constexpr int const f{514'229};
namespace {
template <forfun::common::concepts::addition_unpromoted T>
auto dummy_callback(T const n, T& state) noexcept -> void
{
state += n;
}
} // namespace
TEST_CASE("Fibonacci sequence benchmarking", "[benchmark][fibonacci_sequence]")
{
using namespace forfun::fibonacci::sequence;
using fn_int_t = decltype(dummy_callback<int>);
using fn_fast_t = decltype(dummy_callback<std::uint_fast32_t>);
ankerl::nanobench::Bench()
.title("Fibonacci sequence")
.relative(true)
.run(
NAMEOF_RAW(slow::fib_seq<int, int, fn_int_t>).c_str(),
[]() noexcept {
int r{0};
slow::fib_seq<int, int, fn_int_t>(f, dummy_callback, r);
ankerl::nanobench::doNotOptimizeAway(r);
}
)
.run(
NAMEOF_RAW(fast::fib_seq<int, int, fn_int_t>).c_str(),
[]() noexcept {
int r{0};
fast::fib_seq<int, int, fn_int_t>(f, dummy_callback, r);
ankerl::nanobench::doNotOptimizeAway(r);
}
)
.run(
NAMEOF_RAW(
fast::fib_seq<std::uint_fast32_t, std::uint_fast32_t, fn_fast_t>
)
.c_str(),
[]() noexcept {
std::uint_fast32_t r{0U};
fast::
fib_seq<std::uint_fast32_t, std::uint_fast32_t, fn_fast_t>(
std::uint_fast32_t{f}, dummy_callback, r
);
ankerl::nanobench::doNotOptimizeAway(r);
}
)
.run(
NAMEOF_RAW(creel::fib_seq<int, int, fn_int_t>).c_str(),
[]() noexcept {
int r{0};
creel::fib_seq<int, int, fn_int_t>(f, dummy_callback, r);
ankerl::nanobench::doNotOptimizeAway(r);
}
)
;
}