From 3136fe62e758f2d85d6b263d672c78766bf2f4c9 Mon Sep 17 00:00:00 2001 From: Omar Boukli-Hacene Date: Mon, 11 Sep 2023 22:10:17 +0200 Subject: [PATCH] Fibonacci sequence loops --- CMakeLists.txt | 4 ++ benchmark/fibonacci_sequence_benchmark.cpp | 76 ++++++++++++++++++++++ include/forfun/fibonacci_sequence.hpp | 19 ++++++ src/fibonacci_sequence.cpp | 9 +++ test/fibonacci_sequence_test.cpp | 17 +++++ 5 files changed, 125 insertions(+) create mode 100644 benchmark/fibonacci_sequence_benchmark.cpp create mode 100644 include/forfun/fibonacci_sequence.hpp create mode 100644 src/fibonacci_sequence.cpp create mode 100644 test/fibonacci_sequence_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1213412..dc5fc11 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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" @@ -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" @@ -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" diff --git a/benchmark/fibonacci_sequence_benchmark.cpp b/benchmark/fibonacci_sequence_benchmark.cpp new file mode 100644 index 0000000..1b66b02 --- /dev/null +++ b/benchmark/fibonacci_sequence_benchmark.cpp @@ -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 + +#include + +#include + +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); + }) + + ; +} diff --git a/include/forfun/fibonacci_sequence.hpp b/include/forfun/fibonacci_sequence.hpp new file mode 100644 index 0000000..fa38272 --- /dev/null +++ b/include/forfun/fibonacci_sequence.hpp @@ -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_ diff --git a/src/fibonacci_sequence.cpp b/src/fibonacci_sequence.cpp new file mode 100644 index 0000000..d6c388a --- /dev/null +++ b/src/fibonacci_sequence.cpp @@ -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 diff --git a/test/fibonacci_sequence_test.cpp b/test/fibonacci_sequence_test.cpp new file mode 100644 index 0000000..e9477a4 --- /dev/null +++ b/test/fibonacci_sequence_test.cpp @@ -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 + +#include "forfun/fibonacci_sequence.hpp" + +TEST_CASE("fibonacci_sequence") +{ + SECTION("Loops") + { + SKIP("See benchmark/fibonacci_sequence_benchmark.cpp"); + } +}