Skip to content

Commit

Permalink
Fibonacci sequence loops
Browse files Browse the repository at this point in the history
  • Loading branch information
oboukli committed Sep 16, 2023
1 parent a32e800 commit 3136fe6
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ project(
add_library(
forfun
SHARED
"include/forfun/fibonacci_sequence.hpp"
"include/forfun/first_missing_positive.hpp"
"include/forfun/fizzbuzz.hpp"
"include/forfun/lru_cache.hpp"
"include/forfun/palindrome.hpp"
"include/forfun/palindromic_number.hpp"
"include/forfun/sonar.hpp"
"src/fibonacci_sequence.cpp"
"src/first_missing_positive.cpp"
"src/fizzbuzz.cpp"
"src/lru_cache.cpp"
Expand Down Expand Up @@ -132,6 +134,7 @@ target_link_libraries(
find_package(Catch2 3 REQUIRED)
add_executable(
tests
"test/fibonacci_sequence_test.cpp"
"test/first_missing_positive_test.cpp"
"test/fizzbuzz_test.cpp"
"test/palindromic_number_test.cpp"
Expand All @@ -154,6 +157,7 @@ find_package(nanobench CONFIG REQUIRED)
add_executable(
benchmark
"benchmark/benchmark.cpp"
"benchmark/fibonacci_sequence_benchmark.cpp"
"benchmark/first_missing_positive_benchmark.cpp"
"benchmark/lru_cache_benchmark.cpp"
"benchmark/palindrome_benchmark.cpp"
Expand Down
76 changes: 76 additions & 0 deletions benchmark/fibonacci_sequence_benchmark.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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>

inline constexpr int const f{514229};
inline constexpr std::uint_fast32_t const uf{f};

static_assert(uf == f);

TEST_CASE(
"fibonacci sequence loops benchmarking", "[benchmark][fibonacci_sequence]")
{
using result_t = std::size_t;

ankerl::nanobench::Bench()
.title("Fibonacci sequence loops")

.run(
"Three variables",
[]() {
result_t r{};
// clang-format off
for (auto i{0}, j{1}, tmp{0};
i <= f;
tmp = j + i, i = j, j = tmp)
{
r += i;
}
// clang-format on
ankerl::nanobench::doNotOptimizeAway(r);
})

.run(
"Two variables",
[]() {
result_t r{};
for (auto i{0}, j{1}; i <= f; j += i, i = j - i)
{
r += i;
}
ankerl::nanobench::doNotOptimizeAway(r);
})

.run(
"Two unsigned variables",
[]() {
result_t r{};
for (std::uint_fast32_t i{0}, j{1}; i <= uf; j += i, i = j - i)
{
r += i;
}
ankerl::nanobench::doNotOptimizeAway(r);
})

.run(
"Two variables by Creel",
[]() {
result_t r{};
// Source: https://youtu.be/IZc4Odd3K2Q?t=949
for (auto i{0}, j{1}; i <= f; j = (i += j) - j)
{
r += i;
}
ankerl::nanobench::doNotOptimizeAway(r);
})

;
}
19 changes: 19 additions & 0 deletions include/forfun/fibonacci_sequence.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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

/// Problem source:
/// https://en.wikipedia.org/wiki/Fibonacci_sequence

#ifndef FORFUN_FIBONACCI_SEQUENCE_HPP_
#define FORFUN_FIBONACCI_SEQUENCE_HPP_

namespace forfun::fibonacci::sequence {

// See benchmark/fibonacci_benchmark.cpp

} // namespace forfun::fibonacci::sequence

#endif // FORFUN_FIBONACCI_SEQUENCE_HPP_
9 changes: 9 additions & 0 deletions src/fibonacci_sequence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// 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 "forfun/fibonacci_sequence.hpp"

// See benchmark/fibonacci_sequence_benchmark.cpp
17 changes: 17 additions & 0 deletions test/fibonacci_sequence_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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 <catch2/catch_test_macros.hpp>

#include "forfun/fibonacci_sequence.hpp"

TEST_CASE("fibonacci_sequence")
{
SECTION("Loops")
{
SKIP("See benchmark/fibonacci_sequence_benchmark.cpp");
}
}

0 comments on commit 3136fe6

Please sign in to comment.